I working out of a step-by-step book. This first part works fine and makes the widgets that I want, but the problem is when I try to edit the layout. The book says to "edit the program started on the previous page. So I thought that I would "import" the file and continue with the layout.
#lottoW=Widgets Module Name
from tkinter import*
window=Tk()
img=PhotoImage(file='lotto.gif')
imgLbl=Label(window,image=img)
label1=Label(window,relief='groove',width=2)
label2=Label(window,relief='groove',width=2)
label3=Label(window,relief='groove',width=2)
label4=Label(window,relief='groove',width=2)
label5=Label(window,relief='groove',width=2)
label6=Label(window,relief='groove',width=2)
getBtn=Button(window)
resBtn=Button(window)
#Geometry:
imgLbl.grid()
label1.grid()
label2.grid()
label3.grid()
label4.grid()
label5.grid()
label6.grid()
getBtn.grid()
resBtn.grid()
#Sustain Window:
window.mainloop()
I have tried to put the second part in the same window as the first part, but it says that "tkinter can't invoke 'grid' command." When I edit it in a new window and import the first module, the app will run how the first module did but will not have the edited layout that I wanted. Upon closing the app I will then get an error message telling me that 'imgLbl is not defined.' Where have I gone wrong?
import lottoW
#Geometry
imgLbl.grid(row=1,column=1,rowspan=2) #this line is where problem is
label1.grid(row=1,column=2,padx=10)
label2.grid(row=1,column=3,padx=10)
label3.grid(row=1,column=4,padx=10)
label4.grid(row=1,column=5,padx=10)
label5.grid(row=1,column=6,padx=10)
label6.grid(row=1,column=7,padx=(10,20))
getBtn.grid(row=2,column=6,columnspan=4)
resBtn.grid(row=2,column=6,columnspan=2)
CodePudding user response:
Maybe i don't really understand what you're trying to ask here, but the following code works just fine.
import tkinter as tk
window = tk.Tk()
img = tk.PhotoImage(file='lotto.gif')
imgLbl = tk.Label(window, image=img)
label1 = tk.Label(window, relief='groove', width=2)
label2 = tk.Label(window, relief='groove', width=2)
label3 = tk.Label(window, relief='groove', width=2)
label4 = tk.Label(window, relief='groove', width=2)
label5 = tk.Label(window, relief='groove', width=2)
label6 = tk.Label(window, relief='groove', width=2)
getBtn = tk.Button(window)
resBtn = tk.Button(window)
#Geometry:
imgLbl.grid(row=1,column=1,rowspan=2)
label1.grid(row=1,column=2,padx=10)
label2.grid(row=1,column=3,padx=10)
label3.grid(row=1,column=4,padx=10)
label4.grid(row=1,column=5,padx=10)
label5.grid(row=1,column=6,padx=10)
label6.grid(row=1,column=7,padx=(10,20))
getBtn.grid(row=2,column=6,columnspan=4)
resBtn.grid(row=2,column=6,columnspan=2)
#Sustain Window:
window.mainloop()
CodePudding user response:
I think you are supposed to edit lines between
#Geometry:
and
#Sustain Window:
?