【问题标题】:Tkinter widget not filling gridTkinter 小部件未填充网格
【发布时间】:2015-06-06 04:48:27
【问题描述】:

我似乎在 Tkinter 的网格问题上苦苦挣扎,我是新手,已经浏览了这个论坛和网络,但没有找到答案。

我希望两个按钮和标签填充固定大小为 320 x 240 的网格。我最初遇到了粘性问题,并且阅读了网络以 3 种不同的方式编写它,但没有一个会抛出错误,但它们都不起作用。

这是我的代码:

import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        self.setLounge = 21.0
        tk.Frame.__init__(self, master)
        self.grid()
        self.createWidgets()

    def createWidgets(self):
        self.lou_dec = tk.Button(self)
        self.lou_dec["text"] = "<"
        self.lou_dec["command"] = self.louDec
        self.lou_dec.grid(row=1, column=1, sticky=("N", "S", "E", "W"))

        self.lblLouTemp = tk.Label(self)
        self.lblLouTemp["text"] = self.setLounge
        self.lblLouTemp.grid(row=1, column=2, sticky=(tk.N + tk.S + tk.E + tk.W))

        self.lou_inc = tk.Button(self)
        self.lou_inc["text"] = ">"
        self.lou_inc["command"] = self.louInc
        self.lou_inc.grid(row=1, column=3, sticky=(tk.N, tk.S, tk.E, tk.W))

    def louDec(self):
        self.setLounge -= 0.5
        print ("%s" % self.setLounge)
        fo = open("/home/tony/Code/tempreg.txt", "w")
        fo.write("%s" % self.setLounge)
        fo.close()

    def louInc(self):
        self.setLounge += 0.5
        print ("%s" % self.setLounge)
        fo = open("/home/tony/Code/tempreg.txt", "w")
        fo.write("%s" % self.setLounge)
        fo.close()

root = tk.Tk()
root.title("Heating Controller")
root.geometry("320x240")
app = Application(master=root)
app.mainloop()`

非常感谢

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    查看this 帖子,如果您希望小部件拉伸以适应其环境,似乎有必要为rowconfigurecolumnconfigure 提供weight 参数。在您的情况下,您将不得不这样做两次 - 一次用于框架中的小部件,一次用于框架本身。不要忘记让框架也具有粘性。

    import tkinter as tk
    
    class Application(tk.Frame):
        def __init__(self, master=None):
            self.setLounge = 21.0
            tk.Frame.__init__(self, master)
            master.columnconfigure(0, weight=1)
            master.rowconfigure(0, weight=1)
            self.grid(sticky="news")
            self.createWidgets()
    
        def createWidgets(self):
            self.lou_dec = tk.Button(self)
            self.lou_dec["text"] = "<"
            self.lou_dec["command"] = self.louDec
            self.lou_dec.grid(row=1, column=1, sticky=("N", "S", "E", "W"))
    
            self.lblLouTemp = tk.Label(self)
            self.lblLouTemp["text"] = self.setLounge
            self.lblLouTemp.grid(row=1, column=2, sticky=(tk.N + tk.S + tk.E + tk.W))
    
            self.lou_inc = tk.Button(self)
            self.lou_inc["text"] = ">"
            self.lou_inc["command"] = self.louInc
            self.lou_inc.grid(row=1, column=3, sticky=(tk.N, tk.S, tk.E, tk.W))
    
            for i in range(1,4):
                self.columnconfigure(i, weight=1)
            self.rowconfigure(1, weight=1)
    
        def louDec(self):
            self.setLounge -= 0.5
            print ("%s" % self.setLounge)
            fo = open("/home/tony/Code/tempreg.txt", "w")
            fo.write("%s" % self.setLounge)
            fo.close()
    
        def louInc(self):
            self.setLounge += 0.5
            print ("%s" % self.setLounge)
            fo = open("/home/tony/Code/tempreg.txt", "w")
            fo.write("%s" % self.setLounge)
            fo.close()
    
    root = tk.Tk()
    root.title("Heating Controller")
    root.geometry("320x240")
    app = Application(master=root)
    app.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2016-07-22
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 2019-06-04
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多