任何人都知道如何将 pack() 交换为 place(),我尝试交换它,但运行时它显示为空白

问题描述 投票:0回答:1
import tkinter as tk
from tkinter import ttk



class gift_wrapping_tool(tk.Tk):
    def __init__ (self,*args,**kwargs):
        tk.Tk.__init__(self,*args,**kwargs)
        container = tk.Frame(self,bg="#222448")
        
        container.place(width=500,height=500)
        
        
        self.frames = {}
        
        for F in (FirstPage, PageOne,PageTwo,PageThree):
            
            frame = F(container, self)
            self.frames[F] = frame
            frame.place(width=500, height=500)
            frame.grid(row=0,column=0,sticky="nsew")
        
        self.show_frame(FirstPage)
    
    def show_frame(self,cont):
        frame = self.frames[cont]
        frame.tkraise()
        
        
class FirstPage (tk.Frame):
    
    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent,bg="#222448")
        label = ttk.Label(self,text=" Enter dimensions page:",foreground="light cyan", background="#222448")
        label.place(x=10,y=10)
        
        var1 = tk.StringVar(self) 
        radiobtn1= tk.Radiobutton(self, text="Cuboid", variable=var1, value="Cuboid",bg='light cyan',width=10)
        radiobtn1.place(x=10,y=320)
        
        radiobtn2= tk.Radiobutton(self, text="Cube", variable=var1, value="Cube",bg='light cyan',width=10)
        radiobtn2.place(x=10,y=350) 
        
        radiobtn3= tk.Radiobutton(self, text="Cylinder", variable=var1, value="Cylinder",bg='light cyan',width=10)
        radiobtn3.place(x=10,y=370)
        
        labela= ttk.Label(self, text="Select shape:",background='#222448',foreground='light cyan',width=13)
        labela.place(x=10,y=300)
        
        button1= tk.Button(self,text="Next", bg="#54527E", fg='light cyan',command= lambda: controller.show_frame(PageOne))
        button1.place(x=400,y=400)
        
        
        
        canvas = tk.Canvas(self,bg="#222448")
        canvas.place(x=500,y=500)
        
        
        
        
        
class PageOne(tk.Frame):
        
        def __init__ (self, parent, controller):
            tk.Frame.__init__(self,parent,bg="#222448")
            label = ttk.Label(self,text="Select pattern page:",foreground="light cyan", background="#222448")
            label.place(x=10,y=10)
            
            button2= tk.Button(self,text="Back",bg="#54527E", fg='light cyan',command= lambda: controller.show_frame(FirstPage))
            button2.place(x=10,y=400)
            
            button2a= tk.Button(self,text="Next to booking",bg="#54527E", fg='light cyan',command= lambda: controller.show_frame(PageTwo))
            button2a.place(x=400,y=400)
            
            canvas = tk.Canvas(self,bg="#222448")
            canvas.place(x=500,y=500)
            
class PageTwo(tk.Frame):
        
        def __init__ (self, parent, controller):
            tk.Frame.__init__(self,parent,bg="#222448")
            label = ttk.Label(self,text="Select booking slots page:",foreground="light cyan", background="#222448")
            label.place(x=10,y=10)
            
            button3= tk.Button(self,text="Back to first page" ,bg="#54527E", fg='light cyan',command= lambda: controller.show_frame(FirstPage))
            button3.place(x=10,y=400)
            
            button4= tk.Button(self,text="Back to pattern",bg="#54527E", fg='light cyan',command= lambda: controller.show_frame(PageOne))
            button4.place(x=10,y=380)
            
            button4a= tk.Button(self,text="Next to cost",bg="#54527E", fg='light cyan',command= lambda: controller.show_frame(PageThree))
            button4a.place(x=400,y=400)
            
            canvas = tk.Canvas(self,bg="#222448")
            canvas.place(x=500,y=500)

class PageThree(tk.Frame):
        
        def __init__ (self, parent, controller):
            tk.Frame.__init__(self,parent,bg="#222448")
            label = ttk.Label(self,text="Cost calculation:",foreground="light cyan", background="#222448")
            label.place(x=10,y=10)
            
            button5= tk.Button(self,text="Back to booking",bg="#54527E", fg='light cyan',command= lambda: controller.show_frame(PageTwo))
            button5.place(x=10,y=400)
            
            canvas = tk.Canvas(self,bg="#222448")
            canvas.place(x=500,y=500)
            
            
            
        

app= gift_wrapping_tool()
app.mainloop()

我将

pack()
替换为
place()
,结果显示为空白

python tkinter
1个回答
0
投票

使用

.place()
放置小部件不会调整其父级的大小。因此
FirstPage
PageOne
等实例的默认大小为 1x1 像素。

您可以添加:

container.rowconfigure(0, weight=1)
container.columnconfigure(0, weight=1)

gift_wrapping_tool.__init__()
内部使这些框架扩展到
container
的大小,因为您在
sticky="nsew"
中使用了
frame.grid(row=0, column=0, sticky="nsew")

© www.soinside.com 2019 - 2024. All rights reserved.