2

我正在尝试 Tkinter 并使用网格编写一个小窗口。

代码如下:

from Tkinter import *
from modules.logic import game
import options


class StartWindow:

    def __init__(self):


        data = open("myData", "w")
        data.close()
        self.master = Tk()
        self.l0 =Label (self.master, text = "W=jump", bg = "magenta", font = ("comic sans ms", 20, "bold")).grid(row=0, sticky = W)
        self.l1=Label (self.master, text = "A=left",bg = "magenta",font = ("comic sans ms", 20, "bold")).grid(row = 1, sticky = W)
        self.l2=Label(self.master, text = "D=Duck", bg = "magenta",font = ("comic sans ms", 20, "bold")).grid(row=2, sticky = W )
        self.l3=Label(self.master, text = "Mouse = Shoot", bg = "magenta",font = ("comic sans ms", 20, "bold")).grid(row=3, sticky = W)
        self.l4=Label(self.master, text = "S=Duck", bg = "magenta",font = ("comic sans ms", 20, "bold")).grid(row=4, sticky = W)
        self.l5=Label(self.master, text="Seed:", bg = "magenta",font = ("comic sans ms", 20, "bold")).grid(row=5, sticky = W)
        self.master.minsize(50, 50)
        self.master.weight = 2
        self.master.title("Fluffocalypse")
        self.master.iconify()    
        self.e1 = Entry(self.master)
        self.e1.grid(row=5, column=1, sticky = W)
        self.b1 =Button(self.master, text = "Start", command = self.startGameNormally).grid(row = 6, sticky = W)
        self.b2 = Button(self.master, text = "alten Spielstand laden", command =self.loadOldGame).grid(row = 7, sticky = W)
        mainloop()

它确实有效。

好吧,首先,对我来说,文本看起来不像漫画,但这是一个小问题。

如果您尝试此代码(请执行此操作),您会看到您得到的是一个带有标签和按钮的窗口,就在我放置它们的位置,我也设置了洋红色。

但是,窗口上的空闲空间仍然是灰色的。我也想要自由空间洋红色。这可能吗?如果可以,怎么办?关于如何更改任何已打包内容的颜色有十几种解释,但我没有使用 pack() 而是使用网格,而且我找不到关于如何为整个窗口着色的解决方案,而不仅仅是我的标签和按钮是。

4

2 回答 2

5

我想您希望窗口的背景颜色为magenta

self.master = Tk()                                                      
self.master.configure(bg = 'magenta')

结果:

洋红色窗口

于 2013-03-10T00:03:27.010 回答
2

只需将此行添加到您的构造函数:

self.master.configure(background="magenta")
于 2013-03-10T00:02:58.013 回答