【发布时间】:2020-11-19 19:43:49
【问题描述】:
下面的代码有问题。我在一个讲师完成的 udemy 课程上看到了这段代码,他使用.pack() 方法在 tkinter 窗口中定义了小分区。
问题是我需要在后面使用.grid(),由于这两种方法不能在同一个程序中使用,我需要将.pack()转换为.grid(),但我没有想法如何做到这一点。怎么做?
from tkinter import *
import time
from tkinter import ttk
#~~~~~~~~~~~~~~Defining the window~~~~~~~~~~~~~
root=Tk()
root.title("Parent window")
root.geometry('1600x800+0+0')
root.configure(bg='#FFFFFF')
#~~~~~~~~~~~~~~window~partition~~~~~~~~~~~~~~~~
top=Frame(root, width=1600, height=100, bg='blue', relief=SUNKEN)
top.pack(side=TOP) #i need to change this .pack() to something else to make the program compatible with .grid() for all the lines
w1=Frame(root, width=800, height=700, bg='purple', relief=SUNKEN)
w1.pack(side=LEFT)
w2=Frame(root, width=300, height=700, bg='green', relief=SUNKEN)
w2.pack(side=RIGHT)
w3=Frame(root, width=35, height=700, bg='orange', relief=SUNKEN)
w3.pack(side=LEFT)
w4=Frame(root, width=100, height=700, bg='pink', relief=SUNKEN)
w4.pack(side=LEFT)
root.mainloop()
【问题讨论】:
-
" 因为这两种方法不能在同一个程序中使用" - 这不是一个真实的陈述。它们可以在同一个程序中使用,但它们不能同时用于具有相同主控的小部件。
-
啊,是的,感谢您指出这一点,对不起,我是 python 编码的新手。