Alan Gauld schrieb am Donnerstag, 8. April 2021 um 15:40:19 UTC+2: > On 08/04/2021 06:01, Mohsen Owzar wrote: > > >> 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. > > Because I'm a newbie in Python and write programs since > > a couple of months, and I'm not so familiar with classes, > OK, In that case you should probably read up on classes > and play around with them to get used to the ideas of > classes, objects and methods. > > You can carry on using functions but you will need to > keep track of quite a lot of global variables which > can get messy in bigger programs. Classes just keep > things a bit more tidy. > > 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. > One of the problems in your code is that you are not > storing references to the widgets you create. You are > relying on the containment tree to store the references > and keep them alive. But that makes it difficult to > access. As a general rule of thumb if you are creating > any widget that responds to events you should keep > a reference to it - ie. create a variable. > > For example in your code you have a section like this: > > def gen_t2(frame): > def getValue(event):... > lbl = Label(frame, text='Val').pack() > > ent = Entry(frame) > ent.pack() > ent.insert(0, '2') > > ent.bind('<Return>', getValue) > And you call it like this: > > if firstTime2 == 1: if firstTime2 == 1: > gen_t2(tab2) > firstTime2 = 0 > > gen_t2(tab2) > firstTime2 = 0 > > Inside the function you store a reference to the Entry as ent. > but ent disappears as soon as the function ends, you cannot > use ent to access your entry outside the function. > You need to return the widget like so: > > def gen_t2(frame): > def getValue(event):... > > ... > ent.bind('<Return>', getValue) > return ent > > And then call it like: > > if firstTime2: > entry_field = gen_t2(tab2) > firstTime2 = False > > Now you can access your Entry field via the global > variable entry_field. > > I suspect you should forget about the classes for now, focus > on getting the functions to work, especially returning values > and storing those in global variables that you can access from > elsewhere. These principles are just as important when you > get round to studying classes later. > > And remember that global variables in one module can be > accessed from another module by importing the first module > into the second and using the module name as prefix. > > import tab2 > > txt = tab2.entry_field.get() > > HTH > > You might also find the functions, namespaces and GUI sections > of my tutorial useful (see below). > -- > 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
Hello Alan, I'm so happy that you have so good explained to me, what I have to do. Now, it is a bit clearer how I can use the variables or references in another modules. I try to follow your suggestions to see if I can solve my problem. Again thank you very much. Best regards Mohsen -- https://mail.python.org/mailman/listinfo/python-list