Tkinter 小部件未填充网格
Tkinter widget not filling grid
我似乎正在为 Tkinter 中的网格而苦苦挣扎,我是新手并且已经搜索了这个论坛和网络但没有找到答案。
我想要这两个按钮和标签来填充固定大小为 320 x 240 的网格。我最初遇到了粘性问题,并且阅读了以 3 种不同方式编写的网络,none 其中抛出错误,但 none 有效。
这是我的代码:
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()`
非常感谢
查看 this post,似乎有必要为 rowconfigure
和 columnconfigure
提供一个 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()
我似乎正在为 Tkinter 中的网格而苦苦挣扎,我是新手并且已经搜索了这个论坛和网络但没有找到答案。
我想要这两个按钮和标签来填充固定大小为 320 x 240 的网格。我最初遇到了粘性问题,并且阅读了以 3 种不同方式编写的网络,none 其中抛出错误,但 none 有效。
这是我的代码:
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()`
非常感谢
查看 this post,似乎有必要为 rowconfigure
和 columnconfigure
提供一个 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()