我似乎在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()`
非常感谢
答案 0 :(得分:6)
查看this帖子,如果您希望窗口小部件延伸到weight
和rowconfigure
,则有必要提供columnconfigure
参数适合它的环境。在您的情况下,您将不得不两次执行此操作 - 一次用于框架中的小部件,一次用于框架本身。不要忘记让框架变得粘稠。
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()