Python Forum
[Tkinter] I don't think I understand the basics of tkinter!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] I don't think I understand the basics of tkinter!
#1
The documentation doesn't help!

If I have (forget the variables except the ones I need help with and not all statements are shown):
 root = tk.Tk()
root.geometry('%sx%s+%s+%s' % (Xsize, Ysize, Xoffset, Yoffset))
label=tk.Label(root, textvariable=time_str, font=label_font, bg=bg_color, 
    fg=Number_Color.get(), relief='raised', bd=3)
label.pack(fill='x', padx=5, pady=5)
label.config(fg=Number_Color.get())
root.mainloop()
Questions:
  1. Does the word root have any particular meaning. Can I call it "xyz"?
  2. I assume the "root" in line 1 and the "root" in line 2 have to be the same name. True?
  3. Does "label" have any specific meaning? Can I call it "abc"?
  4. Why do some things use "root." and others use "label."?
  5. Does "root.mainloop" (line 7) have any relationship to the "root=" (line 1) or could it be "def"?
  6. If I call a function, do I have to pass it the "root" and "label" names (or "global" them) or is that implicit?
  7. If I want two different independent tk labels, do I call one "root1" & "label1" and the other "root2" and "label2" to use them independently? Can I use myroot[0] and mylabel[0] and myroot[1] and mylabel[1] so I can select either of them by selecting 0 or 1?
  8. If I have two different but the same type of widget (like two Label widgets) and I want to call a single function to update the contents, geometry, etc. of the one selected, would I pass the "root" and "label" names as parameters so the function operated on the referenced one?
  9. If I have a Label at a location and want to make it smaller, do I just use the modification commands like label.config or should I destroy the label and create a new one? Why?
  10. What happens if I put one label over another? If I destroy the second, will the first one show on the screen?
  11. If I have a certain picture for a background on the screen from outside python and I create a label then either remove it or the program ends, will the background be there or will there be "holes" where the label was which means I have to restore the background? What if the python program is "kill"ed?
Reply
#2
I don't understand tkinter much either

  1. you can call it whatever you wan't it just a variable name that stores an instance of tk.Tk().
  2. root in line 2 is a pointer to the instance of tk.Tk() created in line 1.
  3. again a variable with a pointer to the created label. you can call it whatever you like just ideally make it something sensible to what the object its pointing at is.
  4. when you use root its calling methods of the tk.Tk() instance, label is calling methods on a instance of tk.Label.
  5. yes its an instance of tk.Tk() and its calling its mainloop() which starts the event loop.
  6. you only have to pass root or label to functions that need to use the methods of those instance or use them as a parent to another gui widget etc.
  7. i think you only ever create one instance of tk.Tk(), create as many label as you like and store pointers to them in any variable, list, dictionary etc you like.
  8. just pass the pointer to the label to the function and then call the methods on it.
  9. i would say just modify it.
  10. don't know for sure as i don't do tkinter but id image the first would show if you delete the one you put over the top.
  11. i don't understand what your asking on this one sorry.
Reply
#3
Quote:11. i don't understand what your asking on this one sorry.
If I have a background that's just a black field. Then I create a label. If I remove that label, will the background behind it be restored to the black color? Seems to be "yes" in Gnome system but not necessarily always in my target openbox system. I asked in another question about the ability to leave behind a label even though the creating program has terminated. I want to be able to do both at varying times.

For #7, if I only have one instance of tk.Tk(), like root=tk.Tk() then every time I execute a command like root.geometry, then that would affect all labels, right? I don't want that. I want it to only affect one label.
Reply
#4
You also need to understand that tkinter is not python/pythonic. It is only a wrapper around Tcl/Tk, an award winning software by John Ousterhout. Tk has its own philosophy, its own design and its own language. Tkinter is thus an hybrid python module combining two different approaches and cultures. You can perhaps learn a lot by reading Tcl/Tk literature.
Reply
#5
(Apr-07-2019, 05:46 PM)Gribouillis Wrote: You also need to understand that tkinter is not python/pythonic. It is only a wrapper around Tcl/Tk, an award winning software by John Ousterhout. Tk has its own philosophy, its own design and its own language. Tkinter is thus an hybrid python module combining two different approaches and cultures. You can perhaps learn a lot by reading Tcl/Tk literature.
I have been reading the literature and I still don't understand. I wouldn't ask the question if I hadn't searched the net for answers, read the appropriate parts of the tkinter documentation or saw answers but just couldn't relate them to my situation. I'll bet I've been using the net longer than you have; I have an advanced degree in engineering and I do know how to find things. It's just that python and tkinter are new to me; I have a short time to implement this project; have spent a lot of time on it; I'm doing it as a volunteer and have concluded that the only way to do any of this is by adapting previously-written python programs written without any graphical output.
Reply
#6
Knowledge is acquired bit by byte. I've read through your posts. I suggest Mark Lutz's book learning python(no tkinter in it), then his book programming python(lots of tkinter examples and explanations). Mark has an example of a photo edit gallery all in tkinter (pyphoto). Or john grayson's tkinter programming, super old but still relevant.
Reply
#7
(Apr-08-2019, 08:53 PM)joe_momma Wrote: Knowledge is acquired bit by byte. I've read through your posts. I suggest Mark Lutz's book learning python(no tkinter in it), then his book programming python(lots of tkinter examples and explanations). Mark has an example of a photo edit gallery all in tkinter (pyphoto). Or john grayson's tkinter programming, super old but still relevant.
I found an easier way and I finished the work early. I just dumped any attempt to create multiple windows or worry about the other issues and simply used a main program with no tkinter opening multiple python tkinter programs, each of which controls a specific line of output on the screen. Some (like lines output) are persistent. Others, like data provided by the user, are constantly changing. I simply provide all the data in arguments to a Popen then, to update, I kill the appropriate process and restart it with the new data. I also figured out how to send multiple graphics next to one another. Simple! I found a font that contains the few graphics characters I need, then Popen the program with the font and text I want (widget position, text, font, font size and fg/bg colors are all passed parameters) and it outputs graphics perfectly lined up. I use signals to kill appropriate program (4 output & 4 user data) then create a new one.

That's the difference between being an engineer and a software developer. It's done; it works; It's flexible; it's inelegant (so what!); it is inefficient (a quad core Raspberry Pi with nothing else to do means "so what #2").

Thanks for everyone's help. Without it, I wouldn't have thought "Why do I need to do these things?" and would have learned OOP, Python and Tkinter over the next six months but then I'd have missed my deadline. Finished a month early. But the code would have been a lot nicer and I would have learned something I won't. Then again, at my age and my ability these days to retain information, there would be no guarantee I wouldn't be asking the same questions again.

I'm not being sarcastic. Just practical. And in my 8 discussions I have learned a lot and sped up the process considerably.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020