执行时空白tkinter寡妇

时间:2018-10-12 13:05:28

标签: python tkinter

我正在尝试创建一个简单的应用程序以动态创建tkinter窗口。我使用grid_propagate将框架隐藏在顶部框架之下,但是这样做却得到了一个空白窗口。

from Tkinter import *
class window:
    Window=None
    def __init__(self):
        self.Window = Tk()
        self.Window.geometry("300x200")
    def show(self):
        self.Window.mainloop()

class frame:
    frame = None
    row = 0
    column = 0
    variable = {}

    def __init__(self,window=None):
        self.frame = Frame(window.Window)
        self.frame.grid(row=0,column=0)
        self.frame.grid_propagate(0)

    def moverow(self,no):
        self.row = self.row + no
        self.column = 0

    def movecolumn(self,no):
        self.column = self.column + no

    def checkbox(self, var, label):
        variable = IntVar()
        Checkbutton(self.frame,text=label,variable=variable).grid(row=self.row,column=self.column)
        self.variable[var] = variable

    def display(self, llabel, rlabel):
        Label(self.frame,text=llabel).grid(row=self.row,column=self.column)
        Label(self.frame,text=rlabel).grid(row=self.row,column=self.column+1)

    def nextbutton(self,label,anotherframe):
        button = Button(self.frame,text=label,command= lambda: anotherframe.show())
        button.grid(row=self.row,column=self.column)

    def button(self,label,function):
        button = Button(self.frame,text=label,command= lambda: function())
        button.grid(row=self.row,column=self.column)

    def show(self):
        self.frame.tkraise()


window1 = window()
frame1 = frame(window1)
frame1.display("name:","baarath")
frame2 = frame(window1)
frame2.display("frame:","2")
frame2.moverow(1)
frame2.nextbutton("next",frame1)
frame2.moverow(1)
frame2.checkbox("male","male")
frame2.show()
window1.show()

1 个答案:

答案 0 :(得分:0)

正如jasonharper解释的那样,以下代码应该可以工作:

from Tkinter import *
class window:
    Window=None
    def __init__(self):
        self.Window = Tk()
        self.Window.geometry("300x200")
    def show(self):
        self.Window.mainloop()

class frame:
    frame = None
    row = 0
    column = 0
    variable = {}

    def __init__(self,window=None):
        self.frame = Frame(window.Window)
        self.frame.grid(row=0,column=0)

    def moverow(self,no):
        self.row = self.row + no
        self.column = 0

    def movecolumn(self,no):
        self.column = self.column + no

    def checkbox(self, var, label):
        variable = IntVar()
        Checkbutton(self.frame,text=label,variable=variable).grid(row=self.row,column=self.column)
        self.variable[var] = variable

    def display(self, llabel, rlabel):
        Label(self.frame,text=llabel).grid(row=self.row,column=self.column)
        Label(self.frame,text=rlabel).grid(row=self.row,column=self.column+1)

    def nextbutton(self,label,anotherframe):
        button = Button(self.frame,text=label,command= lambda: anotherframe.show())
        button.grid(row=self.row,column=self.column)

    def button(self,label,function):
        button = Button(self.frame,text=label,command= lambda: function())
        button.grid(row=self.row,column=self.column)

    def show(self):
        self.frame.tkraise()


window1 = window()
frame1 = frame(window1)
frame1.display("name:","baarath")
frame2 = frame(window1)
frame2.display("frame:","2")
frame2.moverow(1)
frame2.nextbutton("next",frame1)
frame2.moverow(1)
frame2.checkbox("male","male")
frame2.show()
window1.show()

此代码将删除行self.frame.grid_propogate(0)。该行导致程序失败的原因是,它阻止了框架调整大小以适应其内容的自然能力,这意味着保持该帧大小等于调用该行后的大小(在本例中为0x0)。有关更多信息,请参见this page