Python tkinter框架没有正确显示

时间:2017-09-05 19:22:36

标签: python tkinter

当我取消注释以main_frame= self.create_widget开头的行时,banner_strip框架不显示....

如果该行被注释掉,那么它就会显示为我想要它。此外,当我将row = 0而不是1传递给self.banner_strip = self.create_widget()然后取消注释上面提到的线条时,此条形框架会显示但位于标签上方框架的中间位置'这是一些文字' 39 ;.知道我在这里做错了什么吗?

from tkinter import *

class Application():
    def __init__(self, master):

        self.master = master
        self.master.geometry("700x400+250+150")
        self.master.resizable(False, False)

        self.banner_frame = self.create_widget(master_widget = self.master, widget = Frame(), width = 700, height = 70, bg = '#255869', row = 0, column = 0, bd = 0)
        self.banner_label = self.create_widget(master_widget = self.banner_frame, widget = Label(), bg = '#255869', row = 0, column = 0, pady = 20, text = "This is some text")
        self.banner_strip = self.create_widget(master_widget = self.banner_frame, widget = Frame(), width = 700, height = 10, bg = '#BCC947', row = 1, column = 0)
        #self.main_frame = self.create_widget(self.master, widget = Frame(), width = 700, height = (400 - 80), bd = 0, row = 1, column = 0)

    def create_widget(self, master_widget = None, widget = None, width = 0, height = 0, bg  = None, fg = None, row = 0, column = 0, text = None, bd = None, padx = 0, pady = 0 ):
        widget_type = type(widget)(width = width, height = height, bg = bg, fg  = fg, text = text, bd = bd)
        widget_type.grid(row = row, column = column)
        return widget_type

cl = Tk()
c = Application(cl)
cl.mainloop()

如果有人知道解决方案,请帮助我。谢谢!

编辑:这是完全相同的程序,但不使用方法create_widget()。在这一个label_frame row=0main_frame row=1完美无缺

from tkinter import *

class Application():
    def __init__(self, master):
        self.master = master
        self.master.geometry("700x400+250+150")
        self.master.title("GetCaseQob v2.1")

        self.master.resizable(False, False)

        self.banner_frame = Frame(master, width = 700, height = 70, bd = 0, bg = '#255869')
        self.banner_frame.grid(row = 0, column = 0)

        self.banner_strip = Frame(self.banner_frame, width = 700, height = 10, bd = 0, bg = '#BCC947')
        self.banner_strip.grid(row = 1, column = 0)

        self.banner = Label(self.banner_frame, bg = '#255869', text = 'This is some text')
        self.banner.grid(row = 0, column = 0, pady = 20)

        self.main_frame = Frame(master, width = 700, height = (400-70), bd = 0)
        self.main_frame.grid(row = 1, column = 0)
        self.main_frame.grid_propagate(0)

cl = Tk()
c = Application(cl)
cl.mainloop()

1 个答案:

答案 0 :(得分:0)

更新

看来真正的问题是你错过了widget_type = type(widget)行中的一个变量。您没有使用master_widget来定义每个小部件。

变化:

widget_type = type(widget)(width = width, height = height, bg = bg, fg  = fg, text = text, bd = bd)

要:

widget_type = type(widget)(master_widget, width = width, height = height, bg = bg, fg  = fg, text = text, bd = bd)

这将纠正您的框架问题,但您会看到布局间距的一些更改。这也必须处理。

因为我不确定你为什么要按照你的方式设置你的小部件,所以我用一种更传统的方式来创建你的代码的修改版本。

from tkinter import *

class Application():
    def __init__(self, master):

        self.master = master
        self.master.geometry("700x400+250+150")
        self.master.resizable(False, False)

        self.banner_frame = Frame(self.master, width = 700, height = 70, bg = '#255869', bd = 0)
        self.banner_frame.grid(row = 0, column = 0)
        self.banner_label = Label(self.banner_frame, bg = '#255869', text = "This is some text")
        self.banner_label.grid(row = 0, column = 0, pady = 20)
        self.banner_strip = Frame(self.banner_frame, width = 700, height = 10, bg = '#BCC947')
        self.banner_strip.grid(row = 1, column = 0)
        self.main_frame = Frame(self.master, width = 700, height = (400 - 80), bd = 0)
        self.main_frame.grid(row = 1, column = 0)


cl = Tk()
c = Application(cl)
cl.mainloop()