Need tips on how to properly organize tkinter script

  • Context: Python 
  • Thread starter Thread starter Eclair_de_XII
  • Start date Start date
  • Tags Tags
    Tips
Click For Summary

Discussion Overview

The discussion revolves around organizing a Tkinter script for a GUI application, specifically addressing issues related to event binding and widget management. Participants share their experiences and suggest alternative approaches or libraries for GUI development.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant expresses difficulty in binding the Home and End keys to navigate a listbox and seeks advice on systematic widget generation within a class structure.
  • Another participant questions the necessity of using Tkinter, suggesting a web-based GUI with Flask as an alternative.
  • A participant clarifies their use of Tkinter is for a calculator application, not for graphics display.
  • Links to external resources are provided by one participant, offering examples of Tkinter code organization for a simple calculator.
  • Another participant recommends considering the wx library as an alternative to Tkinter, highlighting its native controls and active development.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the best approach to organizing the Tkinter script, with multiple competing views on whether to continue using Tkinter or explore alternative libraries.

Contextual Notes

Participants mention issues with event binding and widget management, but do not resolve the underlying technical challenges or assumptions related to their code structure.

Eclair_de_XII
Messages
1,082
Reaction score
91
TL;DR
I'm new to using tkinter. You can find an example of a script I wrote below. Basically, what this script does is create a window with four frames:
i. upper-left: example Label
ii. upper-right: example Label
iii. lower-left: List of numbers bound to <Enter>
iv. lower-right: Range of numbers generated when item from (iii) selected and <Enter> pressed
Basically, I feel like there are two problems that I feel need fixing. But I haven't a clue on how to fix them.

The first of my troubles is a technical one: I want to map the Home and End keys so that the selection on the list on item (iv) goes to the first and last items on the list, respectively. But it isn't working for some reason. I don't know why.

Every time I want to bind an event to the creation of another widget, I have to define a function declaring said widget before the binding. And if I want to declare another widget within the function, I have to create another function within the function. In short, this is not really ideal for me. The code will become messy in no time, I should think, with the number of generated widgets. I'd like suggestions on how to generate widgets with events in a more systematic way. Ideally, I can just put all the widgets to be generated in a class, and call them that way.

I'm somewhat new to programming and what-not, so any other tips regarding my code etiquette will be greatly appreciated. I want to note also, that the Frame keyword-arguments are arbitrary and are not really important to this example. They're just there because I was experimenting earlier. Thanks.

[CODE lang="python" title="Example" highlight="51"]from tkinter import *

root=Tk()
root.title('Frames')
#root.wm_minsize(width=400,height=400)
root.wm_resizable(width=False,height=False)
#root.bind_all('<End>',lambda arg: root.destroy())

d={}
#d['height']=400
#d['width']=400
k=5
d['padx']=k
d['pady']=k
d['borderwidth']=2
d['relief']='sunken'
frame=Frame(root,**d)
frame.grid(row=0,column=0)

label=Label(frame,text='This is frame 1, see.')
label.grid()

frame1=Frame(root,**d)
frame1.grid(row=0,column=1)

label1=Label(frame1,text='This is frame 2, see.')
label1.grid()

frame2=Frame(root,**d)
frame2.grid(row=1,column=0)

num_list=tuple(range(100))
listvar=StringVar(value=num_list)

listbox=Listbox(frame2,listvariable=listvar,height=5)
listbox.grid(row=0,column=0)

x=listbox.selection_get

def generate_list(j):
j=int(j)
return StringVar(value=tuple(range(j,j+10+1)))

def generate_listbox2(*args):
listbox.config({'state':DISABLED})
frame3=Frame(root,**d)
frame3.grid(row=1,column=1)
listbox2=Listbox(frame3,listvariable=generate_list(x()),height=3)
listbox2.grid(row=0,column=0)
listbox2.select_set(0)
listbox2.bind('<Home>',lambda *args: listbox2.select_set(0))
def terminate(*args):
y=listbox2.selection_get()
print(y)
root.destroy()
exit()
listbox2.bind('<Return>',terminate)
listbox2.focus()
scrollbar2=Scrollbar(frame3)
scrollbar2.config(command=listbox2.yview)
scrollbar2.grid(column=1,row=0,sticky=N)

listbox.bind('<Return>',generate_listbox2)

scrollbar=Scrollbar(frame2)
scrollbar.config(command=listbox.yview)
scrollbar.grid(in_=frame2,column=1,row=0,sticky=N+S)

listbox.focus()
listbox.select_set(0)

root.mainloop()
[/CODE]
 
Technology news on Phys.org
Are you using tkinter because you need a gui input? Or for some graphics display?

An alternative is a web based gui using flask and a browser.
 
jedishrfu said:
Are you using tkinter because you need a gui input?

I'm working with it because I need a GUI for a calculator of sorts. I don't need to print any graphics.
 
If you aren't wedded to Tkinter, you might want to look up the wx library. It uses native controls, so tends to look a bit better, has a huge range of controls, and is actively developed. There's an excellent demo program that shows most of the widgets in action, and a great many examples online.
 
  • Like
Likes   Reactions: jedishrfu

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K