How to access a variable from one tab in another tab of a notebook?
Hi guys I have written a GUI with two tabs, Tab 1 and Tab 2 as shown below: On Tab 2 should be my settings parameters placed like "Val". If the user types a value greater than 5 in the entry field, the background color of Tab1 should be changed from light green to gray and the button "B1" should be disabled The problem is that I can't use the variable "val" from Tab2 in Tab 1, even if I have defined in every function and file as the global. It brought all the times the error message "val is not defined in Tab 1". You can run the top level "TabTop" to see my GUIs and uncomment the if part in Tab1 to see the error message. Any help is really appreciated Regards Mohsen # Filename: Tab1.py from tkinter import * def gen_t1(frame): f = LabelFrame(frame, text='f', bg='lightgreen') f.pack(expand=True, fill='both') b1 = Button(f, text='B1').pack() ## if val > 5: ##f_enabled = 'disabled' ##f['bg'] = 'lightgray' ## else: ##f_enabled = 'normal' ##f['bg'] = 'lightgreen' ## # Filename: Tab2.py from tkinter import * def gen_t2(frame): def getValue(event): val = ent.get() print(val) lbl = Label(frame, text='Val').pack() ent = Entry(frame) ent.pack() ent.insert(0, '2') ent.bind('', getValue) The code for the top level is: ## # Filename: TabTop from tkinter import * from tkinter import ttk from Tab1 import gen_t1 from Tab2 import gen_t2 global nb firstTime1 = 1 firstTime2 = 1 root = Tk() root.title('Tab-Tester') root.geometry('300x200') nb = ttk.Notebook(root) tab1 = Frame(nb) tab2 = Frame(nb) nb.add(tab1, text ='Tab 1') nb.add(tab2, text ='Tab 2') nb.pack(expand = 1, fill ="both") def on_tab_change(event): global firstTime1, firstTime2 tab = event.widget.tab('current','text') if tab == 'Tab 1': print('Tab 1') if firstTime1 == 1: gen_t1(tab1) firstTime1 = 0 elif tab == 'Tab 2': print('Tab 2') if firstTime2 == 1: gen_t2(tab2) firstTime2 = 0 nb.bind('<>', on_tab_change) root.mainloop() -- https://mail.python.org/mailman/listinfo/python-list
Re: Yield after the return in Python function.
On Tue, Apr 6, 2021 at 12:40 PM Terry Reedy wrote: > > On 4/5/2021 3:32 PM, Chris Angelico wrote: > > > On Tue, Apr 6, 2021 at 5:14 AM Terry Reedy wrote: > >> Python *could* do the same for expresssions: load 'a' (in this case) > >> once into a register or stack slot and use that value consistently > >> throughout the expression. Replacing the eval with the following exec > >> has the same effect. > > > > True, but I think that this would be enough of a semantic change that > > Python should be very VERY careful about doing it. > > I consider it beyond a possibility. > I just realised that the whole eval/exec/namespace stuff is massive overkill. All you need is an object that is inconsistent in its boolification... >>> class Wat: ... def __bool__(self): ... self.state = not getattr(self, "state", False) ... return self.state ... >>> a = Wat() >>> a and not a True ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Presenting our EuroPython 2021 logo
Over the last couple of weeks, we have worked with our designer Jessica Peña to come up with a logo which reflects both our desire to come together online during the pandemic and shows how well our community is interconnected around the world. Here's our brand new logo for EuroPython 2021 Online: https://blog.europython.eu/presenting-our-europython-2020-logo/ Hope you like it :-) We will soon make EuroPython 2021 merchandise available with the new logo in our EuroPython merch shop, so you can order t-shirts, sweatshirts, caps and mugs to show your support and love for the conference. Help spread the word Please help us spread this message by sharing it on your social networks as widely as possible. Thank you ! Link to the blog post: https://blog.europython.eu/presenting-our-europython-2020-logo/ Tweet: https://twitter.com/europython/status/1379750330347499520 Enjoy, -- EuroPython 2021 Team https://www.europython-society.org/ -- https://mail.python.org/mailman/listinfo/python-list
Re: Yield after the return in Python function.
On Wed, Apr 7, 2021 at 12:31 PM Chris Angelico wrote: > > I just realised that the whole eval/exec/namespace stuff is massive > overkill. All you need is an object that is inconsistent in its > boolification... > > Somewhat related: https://bugs.python.org/issue42899 Steve -- https://mail.python.org/mailman/listinfo/python-list
Re: Yield after the return in Python function.
On Wed, Apr 7, 2021 at 10:30 PM Stestagg wrote: > > > > On Wed, Apr 7, 2021 at 12:31 PM Chris Angelico wrote: >> >> >> I just realised that the whole eval/exec/namespace stuff is massive >> overkill. All you need is an object that is inconsistent in its >> boolification... >> > > Somewhat related: https://bugs.python.org/issue42899 > Yup. There are a very few edge cases where pathological behaviour can be optimized out. Sometimes, there's an alternative way to describe it (for example, containment is defined as "identity or equality"), but other times, you can't actually pin down the exact semantics in Python code. Or maybe it's possible, but really hard; the precise meaning of "yield from iterable" is quite the read - check out PEP 380, and notice that even just calling a method isn't as simple as it looks :) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
pandas/jupyther notebook?
linux mint 20 python 3.8 jupyter 1.0.0 jedi 0.18.0 I am teaching myself pandas/jupyter notebooks. The problem I am having is tab autocomplete seems to be working erratically. Googling shows that most people solve autocomplete problems by putting import pandas as pd %config Completer.use_jedi = False in the first cell. I have done that and still have the following problem. if I type: df = pd.read it will drop down a list box that has read_csv in it. If I then type: emp it will fill in employees.csv. If I type: df.h type head, but if I type: df['Gender'] = df['Gender'].ast it will not complete astype. I wonder if someone can tell me why this is happening and maybe how to fix it. Thanks, Jim -- https://mail.python.org/mailman/listinfo/python-list
Re: How to access a variable from one tab in another tab of a notebook?
On 07/04/2021 09:35, Mohsen Owzar wrote: > The problem is that I can't use the variable "val" from Tab2 in Tab 1, > # Filename: Tab1.py > from tkinter import * > > def gen_t1(frame): > f = LabelFrame(frame, text='f', bg='lightgreen') > f.pack(expand=True, fill='both') > > b1 = Button(f, text='B1').pack() > > ## if val > 5: > # Filename: Tab2.py > from tkinter import * > > def gen_t2(frame): > def getValue(event): > val = ent.get() > print(val) Note that val is a local variable within a nested function. It only exists while the nested function is executing. As soon as the function ends val is thrown away. If you want the value of the entry you need to store it in a variable that is visible after the function exits (by returning it perhaps? or setting a global - ugly) But you still have the problem of making that visible to Tab1. You could import tab2 into tab1 and use Tab2.varName But this is why GUIs are often(usually?) built as a class because you can store all the state variables within the instance and access them from all the methods. You can do it with functions but you usually wind up with way too many globals to be comfortable. > from Tab1 import gen_t1 > from Tab2 import gen_t2 ... > def on_tab_change(event): > global firstTime1, firstTime2 > > tab = event.widget.tab('current','text') > if tab == 'Tab 1': > print('Tab 1') > if firstTime1 == 1: > gen_t1(tab1) > firstTime1 = 0 Probably nicer to use boolean values for firstTime1 and firstTime2, ie True and False rather than their numeric equivalents. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos -- https://mail.python.org/mailman/listinfo/python-list
Re: How to access a variable from one tab in another tab of a notebook?
Alan Gauld schrieb am Donnerstag, 8. April 2021 um 02:05:01 UTC+2: > On 07/04/2021 09:35, Mohsen Owzar wrote: > > > The problem is that I can't use the variable "val" from Tab2 in Tab 1, > > # Filename: Tab1.py > > from tkinter import * > > > > def gen_t1(frame): > > f = LabelFrame(frame, text='f', bg='lightgreen') > > f.pack(expand=True, fill='both') > > > > b1 = Button(f, text='B1').pack() > > > > ## if val > 5: > > # Filename: Tab2.py > > from tkinter import * > > > > def gen_t2(frame): > > def getValue(event): > > val = ent.get() > > print(val) > Note that val is a local variable within a nested function. > It only exists while the nested function is executing. > As soon as the function ends val is thrown away. > > If you want the value of the entry you need to store it > in a variable that is visible after the function exits > (by returning it perhaps? or setting a global - ugly) > > But you still have the problem of making that visible > to Tab1. You could import tab2 into tab1 and use Tab2.varName > > But this is why GUIs are often(usually?) built as a class > because you can store all the state variables within > the instance and access them from all the methods. > > You can do it with functions but you usually wind up > with way too many globals to be comfortable. > > from Tab1 import gen_t1 > > from Tab2 import gen_t2 > ... > > def on_tab_change(event): > > global firstTime1, firstTime2 > > > > tab = event.widget.tab('current','text') > > if tab == 'Tab 1': > > print('Tab 1') > > if firstTime1 == 1: > > gen_t1(tab1) > > firstTime1 = 0 > Probably nicer to use boolean values for > firstTime1 and firstTime2, ie True and False > rather than their numeric equivalents. > > HTH > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos Hi Alan G, Thanks a lot for giving me this solution. Because I'm a newbie in Python and write programs since a couple of months, and I'm not so familiar with classes, would be very nice to give me a code, how I have to change my code into a class form. I tried without a class and I ran into problems that the defined frame and entry are not defined. Best regards Mohsen -- https://mail.python.org/mailman/listinfo/python-list