Need tips on how to properly organize tkinter script

  • Python
  • Thread starter Eclair_de_XII
  • Start date
  • Tags
    Tips
In summary, the first problem is that 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.The second problem is that I want 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
  • #1
Eclair_de_XII
1,083
91
TL;DR Summary
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.

Example:
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()
 
Technology news on Phys.org
  • #2
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.
 
  • #3
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.
 
  • #5
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

1. How can I structure my tkinter script to make it more organized?

One approach to organizing a tkinter script is to break it down into smaller functions or classes that handle specific tasks. This will make your code more modular and easier to read and maintain.

2. Is there a best practice for naming variables and functions in tkinter?

It is generally recommended to use descriptive and meaningful names for your variables and functions in tkinter. This will make your code more readable and easier to understand for yourself and others.

3. Should I use global variables in my tkinter script?

It is usually not recommended to use global variables in tkinter, as they can cause unintended side effects and make your code more difficult to debug. Instead, try passing variables as arguments to your functions or using class attributes.

4. How can I keep my tkinter code organized as it grows in complexity?

As your tkinter script becomes more complex, it is important to regularly review and refactor your code. This means breaking it down into smaller functions or classes, eliminating redundant code, and keeping your naming conventions consistent.

5. Are there any tools or methods for debugging and organizing my tkinter script?

Yes, there are various debugging and organizational tools available for tkinter, such as the PDB debugger and the Pylint code analyzer. Additionally, following coding best practices and regularly reviewing and refactoring your code can also help keep it organized and error-free.

Similar threads

  • Programming and Computer Science
Replies
3
Views
824
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Differential Equations
Replies
1
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
  • STEM Academic Advising
Replies
10
Views
4K
Back
Top