Python Need tips on how to properly organize tkinter script

  • Thread starter Thread starter Eclair_de_XII
  • Start date Start date
  • Tags Tags
    Tips
Click For Summary
The discussion highlights two main issues related to using Tkinter for GUI development. The first problem involves mapping the Home and End keys to navigate to the first and last items in a list, which is currently not functioning as intended. The second issue pertains to the complexity of binding events to dynamically created widgets, leading to messy code. The user seeks a more systematic approach for generating widgets and managing their events, ideally through class-based organization.Additionally, there is a suggestion to consider alternative libraries like wxPython, which offers a more native look and a wider range of controls, as well as links to resources for creating a simple calculator using Tkinter. The user expresses a need for a GUI specifically for a calculator, emphasizing that graphics are not required. Overall, the conversation revolves around improving code organization and functionality in Tkinter while exploring potential alternatives.
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 jedishrfu
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K