- #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.
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()