【问题标题】:Keeping the background colour when adding buttons to a grid/frame将按钮添加到网格/框架时保持背景颜色
【发布时间】:2014-01-10 23:20:47
【问题描述】:

我遇到的问题是在向框架添加按钮时保持我的背景颜色,一旦我运行模块,背景颜色就会消失,任何帮助将不胜感激,谢谢。

这是我的代码:

import tkinter
import tkinter as tk

root = tk.Tk()
root.geometry('1000x600')

var=tk.StringVar()

Frame1 = tk.Frame(root)
Frame1.configure(background='light blue',height='300',width='500')
Frame1.grid(row='0',column='0')

Frame2 = tk.Frame(root)
Frame2.configure(background='grey',height='300',width='500')
Frame2.grid(row='0',column='1')

Frame3 = tk.Frame(root)
Frame3.configure(background='grey',height='300',width='500')
Frame3.grid(row='1',column='0')

Frame4 = tk.Frame(root)
Frame4.configure(background='light blue',height='300',width='500')
Frame4.grid(row='1',column='1')


def PrintOrder():
    LabelOrder = tk.Label(Frame3,text="DONUT ORDER")
    LabelOrder.grid(row='0',column='0')
    return

Button1 = tk.Button(Frame1,text="Apple Cinnamon",height='2',width='15',command=PrintOrder).grid(row='0',column='0')
Button2 = tk.Button(Frame1,text="Strawberry",height='2',width='15',command=PrintOrder).grid(row='0',column='1')
Button3 = tk.Button(Frame1,text="Custard",height='2',width='15',command=PrintOrder).grid(row='0',column='2')
Button4 = tk.Button(Frame1,text="Sugar Ring",height='2',width='15',command=PrintOrder).grid(row='1',column='0')
Button5 = tk.Button(Frame1,text="Chocolate Caramel",height='2',width='15',command=PrintOrder).grid(row='1',column='1')
Button6 = tk.Button(Frame1,text="Lemon Circle",height='2',width='15',command=PrintOrder).grid(row='1',column='2')
Button7 = tk.Button(Frame1,text="Blueberry Blaster",height='2',width='15',command=PrintOrder).grid(row='2',column='0')
Button8 = tk.Button(Frame1,text="Strawberry Surprise",height='2',width='15',command=PrintOrder).grid(row='2',column='1')
Button9 = tk.Button(Frame1,text="Simple Sugar",height='2',width='15',command=PrintOrder).grid(row='2',column='2')

Label1 = tk.Label(Frame2,text="Donut special 6 for the price of 5").grid(row='0',column='0')
Button10 = tk.Button(Frame2,text="SPECIAL",height='5',width='20').grid(row='1',column='0')

root.mainloop()

【问题讨论】:

    标签: python python-3.x colors background tkinter


    【解决方案1】:

    你的框架仍然有它的背景颜色。如果你给它一个不同的颜色以便它显示(例如:“红色”),你可以很容易地看到这一点,并在按钮之间添加填充(例如:tk.Button(...).grid(..., padx=10, pady=10). 我认为唯一发生的事情是按钮之间没有空间可以显示颜色,默认行为是框架缩小(或增长)以适应其内容。

    其他问题包括您没有为任何行或列赋予权重,因此它们不会随着主窗口的增大或缩小而增大或缩小。此外,您没有为框架设置sticky 属性,因此它们不会填充它们占据的网格单元。将sticky="nsew" 添加到您对框架进行网格化的位置,您可能会看到更多颜色。

    使用网格时的经验法则是始终为每个项目设置sticky 属性,并为至少一行和一列赋予1(一)的权重。

    【讨论】:

      【解决方案2】:

      您可以在相框上使用grid_propagate(0)。这样,框架的大小就不会调整为小部件的大小。

      在您的代码中,我使用下一行来保持 Frame1 的大小:

      Frame1.grid_propagate(0)
      

      你可以检查一下:

      http://effbot.org/tkinterbook/grid.htm#Tkinter.Grid.grid_propagate-method

      grid_propagate(flag) [#]

      启用或禁用几何传播。启用后,连接到此小部件的网格管理器会在子小部件更改大小时尝试更改小部件的大小。默认情况下始终启用传播。

      标志 为真以启用传播。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-06
        • 2013-09-14
        • 2020-04-08
        • 2013-06-24
        • 1970-01-01
        • 2014-07-25
        • 1970-01-01
        相关资源
        最近更新 更多