我班上的tkinter小部件没有显示

时间:2018-12-24 10:30:50

标签: python python-3.x tkinter

我正在尝试编写一个包含多个页面的代码,当单击一个按钮时可以切换到该代码。它最初可以正常工作,但是我的窗口小部件没有显示,也没有警告或错误消息。其次,使用tk和tk.TK有什么区别?

from tkinter import *
import tkinter as tk

class moreTab(tk.Tk):
    def __init__(self):
        Tk.__init__(self)
        self.geometry("1200x600")
        container = Frame(self, bg='#c9e3c1')
        container.pack(side = "top", fill = 'both', expand = True)
        container.grid_rowconfigure(0, weight = 1)
        container.grid_columnconfigure(0, weight = 1)
        self.frames = {}
        for q in (pageone, widget):
            frame = q(container,self)
            self.frames[q] = frame
            frame.place(x= 0,y = 0)
        self.raise_frame(pageone)
    def raise_frame(self,cont):
        frame = self.frames[cont]
        frame.tkraise()

class widget(Frame):
    def __init__(self, master, control):
        Frame.__init__(self, master)

        lab = tk.Label(self, text="main page")
        lab.place(x = 10, y = 40)
        but = tk.Button(self, text='visit start page', command=lambda: 
control.raise_frame(pageone))
        but.place(x = 10, y = 70)
class pageone(Frame):
    def __init__(self, master, control):
        Frame.__init__(self,master)
        lab = Label(self, text = 'welcome to Game Analysis')
        lab.place(x = 10, y = 10)
        but = Button(self, text = "Start", command  = lambda: 
control.raise_frame(widget))
        but.place(x = 10, y = 20)

app = moreTab()
app.mainloop()

1 个答案:

答案 0 :(得分:0)

问题就出在,您正在使用place()。使用grid几何管理器。同时使用import tkinter as tkfrom tkinter import *是没有意义的。使用一个并保持一致。如果使用后者,则可以使用所有内容,因此您可以写Button(...)。但是,如果使用前者,则必须引用每个小部件,例如tk.Button(...)

import tkinter as tk

class moreTab(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.geometry("1200x600")
        container = tk.Frame(self, bg='#c9e3c1')
        container.pack(side = "top", fill = 'both', expand = True)
        container.grid_rowconfigure(0, weight = 1)
        container.grid_columnconfigure(0, weight = 1)
        self.frames = {}
        for q in (pageone, widget):
            frame = q(container, self)
            self.frames[q] = frame
            frame.grid(row=0, column=0, sticky='nsew')
        self.raise_frame(pageone)

    def raise_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

class widget(tk.Frame):
    def __init__(self, master, control):
        tk.Frame.__init__(self, master)

        lab = tk.Label(self, text="main page")
        lab.grid(row=0, column=0, padx=10, pady=10)

        but = tk.Button(self, text='visit start page', command=lambda: control.raise_frame(pageone))
        but.grid(row=1, column=0, padx=10, pady=10)

class pageone(tk.Frame):
    def __init__(self, master, control):
        tk.Frame.__init__(self, master)
        lab = tk.Label(self, text = 'welcome to Game Analysis')
        lab.grid(row=0, column=0, padx=10, pady=10)
        but = tk.Button(self, text = "Start", command = lambda: control.raise_frame(widget))
        but.grid(row=1, column=0, padx=10, pady=10)

app = moreTab()
app.mainloop()