简体   繁体   English

在Python Tkinter的Tk类中使用Frame类

[英]Using a Frame class in a Tk class in Python tkinter

In the below example, I am trying to use Frame1() in MainW(). 在下面的示例中,我尝试在MainW()中使用Frame1()。 I tried many variations of the below code. 我尝试了以下代码的许多变体。 The problem is that the frame object color and rowspan are not changing at all. 问题在于框架对象的颜色和行距根本没有改变。 I know there is a problem in terms of using Frame1() in MainW(). 我知道在MainW()中使用Frame1()有问题。 Can someone point out the error? 有人可以指出错误吗?

from tkinter import *

class Frame1(Frame):
        def __init__(self, parent):
            Frame.__init__(self, parent, bg="red")
            self.parent = parent
            self.widgets()
        def widgets(self):
            self.text = Text(self)
            self.text.insert(INSERT, "Hello World\t")
            self.text.insert(END, "This is the first frame")
            self.text.grid(row=0, column=0)


class MainW(Tk):
    def __init__(self, parent):
        Tk.__init__(self, parent)
        self.parent = parent
        self.mainWidgets()
    def mainWidgets(self):
        self.label = Label(self, text="Main window label")
        self.label.grid(row=0, column=0)
        self.window = Frame1(self)
        self.window.grid(row=0, column=10, rowspan=2)

if __name__=="__main__":
    app = MainW(None)
    app.mainloop()

Here is the output which is not what I want. 这不是我想要的输出。 I need the frame's background red and rowspan to be 1: 我需要框架的背景红色和行距为1: 在此处输入图片说明

Thank you 谢谢

You can't see frame color because you put widget which fills all frame. 您看不到框架颜色,因为您放置了填满所有框架的小部件。

If you add margins ( padx , pady ) then you can see frame color. 如果添加边距( padxpady ),则可以看到框架颜色。

self.text.grid(row=0, column=0, padx=20, pady=20)

You can't see rowspan because you have empty cell is second row. 您看不到行rowspan因为第二行是空cell Empty cell has no width and height. 空单元格没有宽度和高度。 Add Label in second row and will see how rowspan works. 在第二行中添加标签,将看到行rowspan工作方式。

from tkinter import *

class Frame1(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent, bg="red")
        self.parent = parent
        self.widgets()

    def widgets(self):
        self.text = Text(self)
        self.text.insert(INSERT, "Hello World\t")
        self.text.insert(END, "This is the first frame")
        self.text.grid(row=0, column=0, padx=20, pady=20) # margins


class MainW(Tk):

    def __init__(self, parent):
        Tk.__init__(self, parent)
        self.parent = parent
        self.mainWidgets()

    def mainWidgets(self):

        self.label1 = Label(self, text="Main window label", bg="green")
        self.label1.grid(row=0, column=0)

        self.label2 = Label(self, text="Main window label", bg="yellow")
        self.label2.grid(row=1, column=0)

        self.window = Frame1(self)
        self.window.grid(row=0, column=10, rowspan=2)

if __name__=="__main__":
    app = MainW(None)
    app.mainloop()

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM