Python Tkinter 如何使用网格为窗口着色 [英] Python Tkinter how to color a window using grid

查看:40
本文介绍了Python Tkinter 如何使用网格为窗口着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying out Tkinter and writing a little window, using grid.

代码如下:

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()

确实有效.

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

Well, for one, the text does not look like comic sans to me, but this is a minor problem.

如果您尝试此代码(请这样做),您会看到您得到的是一个带有标签和按钮的窗口,就在我放置它们的位置,我也将它们设置为洋红色.

If you try this code (please do), you see that what you get is a window with labels and buttons just where I placed them, in that magenta color I set them too.

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

However, the free spaces on the window remain grey. I would like to have the free spaces magenta also. Is that possible and if so how? There are a dozen explanations on how to change the color of anything that is packed, but I am not using pack() but grid, and I cannot find a solution as to how color the whole window, not just the space where my labels and buttons are.

推荐答案

我想你希望窗口的背景颜色是 magenta:

I suppose you would like to have the background color of window to be magenta:

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

结果:

这篇关于Python Tkinter 如何使用网格为窗口着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆