我不明白如何在Python Tkinter中使用grid()函数和pack()。我已经看过一个教程,但我无法对我想要的项目进行排序。我使用的是pack功能,但我不知道网格功能是否更好......我还希望小部件根据屏幕大小灵活调整。该应用程序专为7"屏幕(Raspberry pi的tactil屏幕)但也许它可以在另一个屏幕上工作...... 我附上了一个我更喜欢的布局链接,我希望有人可以帮助我!谢谢!!
import Tkinter as tk
from Tkinter import *
import time
TITLE_FONT= ("Verdana", 18)
TEXT_FONT = ("Verdana", 14)
QUESTION_FONT = ("Verdana", 14)
today = time.strftime("%d/%m/%Y")
class Interface(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
#container.pack(side="top", fill="both", expand = True)
container.grid()
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (MenuInici, Page2, Page3):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(MenuInici)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class MenuInici(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
self.label1 = tk.Label(self, text="PAGE1", font=TITLE_FONT,bg="green", fg="black")
self.label2 = tk.Label(self, text=today, font=TITLE_FONT,bg="blue", fg="black")
self.label1.pack(side = TOP, expand =TRUE)
self.label2.pack(side = TOP)
button1 = tk.Button(self, text="Button1(none)", width=15, height=3,
command=self.Iniciar)
button2 = tk.Button(self, text="Button2(page2)",width=15, height=3,
command=lambda: controller.show_frame(Page2))
button3 = tk.Button(self, text="Button3(quit)",width=15, height=3,
command=self.quit)
button4 = tk.Button(self, text="Button4(quit)",width=15, height=3,
command=self.quit)
button5 = tk.Button(self, text="Button5",width=15, height=3,
command=self.quit)
button6 = tk.Button(self, text="Button6(quit)",width=15, height=3,
command=self.quit)
button1.pack(side = TOP,pady=6)
button2.pack(side = TOP,pady=6)
button3.pack(side = TOP,pady=6)
button4.pack(side = RIGHT,pady=6)
button5.pack(side = RIGHT,pady=6)
button6.pack(side = RIGHT,pady=6)
def Iniciar(self,var):
pass
def quit(self):
app.destroy()
class Page2(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="Page2", font=TITLE_FONT).pack()
label2 = tk.Label(self, text=today, font=TITLE_FONT).pack()
button1 = tk.Button(self, text="Page3",
command=lambda: controller.show_frame(Page3)).pack()
button2 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(MenuInici))
button2.pack()
class Page3(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label1 = tk.Label(self, text="Page3", font=TITLE_FONT)
label2 = tk.Label(self, text=today, font=TITLE_FONT)
label1.pack()
label2.pack()
button1 = tk.Button(self, text="Back",
command=lambda: controller.show_frame(MenuInici)).pack()
app = Interface()
w,h = app.maxsize()
app.geometry("%dx%d"%(630,360))
app.title ("Hello")
app.mainloop()
答案 0 :(得分:-1)
如果您在小部件上使用grid
,则必须先使用pack
。举个例子:
from Tkinter import *
root = Tk()
root.title("Test: Pack and Grid")
label = Label(root)
label.pack() # Pack Command: Show my widget 'label' on the screen.
label.grid(row=0, column=0) # Grid Command: I want my widget 'label' to be on the first(0) row and on the first column(0)