继承自 Python tkinter Frame 搞砸包功能
Inherit from a Python tkinter Frame screw up pack functionality
代码如下:
import tkinter as tk
def click(event):
print (event.widget.winfo_name())
sq=250
win = tk.Tk()
topFrame = tk.Frame(win)
middleFrame = tk.Frame(win)
bottomFrame = tk.Frame(win)
top_leftFrame = tk.Frame(topFrame, height=sq, width=sq, bg="sienna")
top_middleFrame = tk.Frame(topFrame, height=sq, width=sq, bg="chocolate")
top_rightFrame = tk.Frame(topFrame, height=sq, width=sq, bg="peru")
middle_leftFrame = tk.Frame(middleFrame, height=sq, width=sq, bg="sandy brown")
middle_middleFrame = tk.Frame(middleFrame, height=sq, width=sq, bg="salmon")
middle_rightFrame = tk.Frame(middleFrame, height=sq, width=sq, bg="rosy brown")
bottom_leftFrame = tk.Frame(bottomFrame, height=sq, width=sq, bg="plum")
bottom_middleFrame = tk.Frame(bottomFrame, height=sq, width=sq, bg="violet")
bottom_rightFrame = tk.Frame(bottomFrame, height=sq, width=sq, bg="purple")
topFrame.pack(side=tk.TOP)
middleFrame.pack(side=tk.TOP)
bottomFrame.pack(side=tk.TOP)
top_leftFrame.pack(side=tk.LEFT)
top_middleFrame.pack(side=tk.LEFT)
top_rightFrame.pack(side=tk.LEFT)
middle_leftFrame.pack(side=tk.LEFT)
middle_middleFrame.pack(side=tk.LEFT)
middle_rightFrame.pack(side=tk.LEFT)
bottom_leftFrame.pack(side=tk.LEFT)
bottom_middleFrame.pack(side=tk.LEFT)
bottom_rightFrame.pack(side=tk.LEFT)
win.bind("<Button-1>", click)
win.mainloop()
因为我无法获得点击的小部件的名称,所以我想通过用新的 method/attribute 制作新的 class 来解决这个问题。但是,除了 pack 的功能外,一切正常。
import tkinter as tk
class Frame_ext(tk.Frame):
def __init__(self, master=None, string=None, cnf={}, **kw):
super().__init__(master=None, cnf={}, **kw)
self.string = string
def click(event):
print (event.widget.string)
sq=250
win = tk.Tk()
topFrame = tk.Frame(win)
middleFrame = tk.Frame(win)
bottomFrame = tk.Frame(win)
top_leftFrame = Frame_ext(topFrame, height=sq, width=sq, bg="sienna", string="1")
top_middleFrame = Frame_ext(topFrame, height=sq, width=sq, bg="chocolate", string="2")
top_rightFrame = Frame_ext(topFrame, height=sq, width=sq, bg="peru", string="3")
middle_leftFrame = Frame_ext(middleFrame, height=sq, width=sq, bg="sandy brown", string="4")
middle_middleFrame = Frame_ext(middleFrame, height=sq, width=sq, bg="salmon", string="5")
middle_rightFrame = Frame_ext(middleFrame, height=sq, width=sq, bg="rosy brown", string="6")
bottom_leftFrame = Frame_ext(bottomFrame, height=sq, width=sq, bg="plum", string="7")
bottom_middleFrame = Frame_ext(bottomFrame, height=sq, width=sq, bg="violet", string="8")
bottom_rightFrame = Frame_ext(bottomFrame, height=sq, width=sq, bg="purple", string="9")
topFrame.pack(side=tk.TOP)
middleFrame.pack(side=tk.TOP)
bottomFrame.pack(side=tk.TOP)
top_leftFrame.pack(side=tk.LEFT)
top_middleFrame.pack(side=tk.LEFT)
top_rightFrame.pack(side=tk.LEFT)
middle_leftFrame.pack(side=tk.LEFT)
middle_middleFrame.pack(side=tk.LEFT)
middle_rightFrame.pack(side=tk.LEFT)
bottom_leftFrame.pack(side=tk.LEFT)
bottom_middleFrame.pack(side=tk.LEFT)
bottom_rightFrame.pack(side=tk.LEFT)
win.bind("<Button-1>", click)
win.mainloop()
有人知道这是什么原因吗?我不确定 class 继承是否正确。
对我来说看起来不错。谢谢
您需要将收到的 master
传递给超类 __init__
。此外,您不需要接受或传递 cnf
.
class Frame_ext(tk.Frame):
def __init__(self, master=None, string=None, **kw):
super().__init__(master=master, **kw)
^^^^^^
代码如下:
import tkinter as tk
def click(event):
print (event.widget.winfo_name())
sq=250
win = tk.Tk()
topFrame = tk.Frame(win)
middleFrame = tk.Frame(win)
bottomFrame = tk.Frame(win)
top_leftFrame = tk.Frame(topFrame, height=sq, width=sq, bg="sienna")
top_middleFrame = tk.Frame(topFrame, height=sq, width=sq, bg="chocolate")
top_rightFrame = tk.Frame(topFrame, height=sq, width=sq, bg="peru")
middle_leftFrame = tk.Frame(middleFrame, height=sq, width=sq, bg="sandy brown")
middle_middleFrame = tk.Frame(middleFrame, height=sq, width=sq, bg="salmon")
middle_rightFrame = tk.Frame(middleFrame, height=sq, width=sq, bg="rosy brown")
bottom_leftFrame = tk.Frame(bottomFrame, height=sq, width=sq, bg="plum")
bottom_middleFrame = tk.Frame(bottomFrame, height=sq, width=sq, bg="violet")
bottom_rightFrame = tk.Frame(bottomFrame, height=sq, width=sq, bg="purple")
topFrame.pack(side=tk.TOP)
middleFrame.pack(side=tk.TOP)
bottomFrame.pack(side=tk.TOP)
top_leftFrame.pack(side=tk.LEFT)
top_middleFrame.pack(side=tk.LEFT)
top_rightFrame.pack(side=tk.LEFT)
middle_leftFrame.pack(side=tk.LEFT)
middle_middleFrame.pack(side=tk.LEFT)
middle_rightFrame.pack(side=tk.LEFT)
bottom_leftFrame.pack(side=tk.LEFT)
bottom_middleFrame.pack(side=tk.LEFT)
bottom_rightFrame.pack(side=tk.LEFT)
win.bind("<Button-1>", click)
win.mainloop()
因为我无法获得点击的小部件的名称,所以我想通过用新的 method/attribute 制作新的 class 来解决这个问题。但是,除了 pack 的功能外,一切正常。
import tkinter as tk
class Frame_ext(tk.Frame):
def __init__(self, master=None, string=None, cnf={}, **kw):
super().__init__(master=None, cnf={}, **kw)
self.string = string
def click(event):
print (event.widget.string)
sq=250
win = tk.Tk()
topFrame = tk.Frame(win)
middleFrame = tk.Frame(win)
bottomFrame = tk.Frame(win)
top_leftFrame = Frame_ext(topFrame, height=sq, width=sq, bg="sienna", string="1")
top_middleFrame = Frame_ext(topFrame, height=sq, width=sq, bg="chocolate", string="2")
top_rightFrame = Frame_ext(topFrame, height=sq, width=sq, bg="peru", string="3")
middle_leftFrame = Frame_ext(middleFrame, height=sq, width=sq, bg="sandy brown", string="4")
middle_middleFrame = Frame_ext(middleFrame, height=sq, width=sq, bg="salmon", string="5")
middle_rightFrame = Frame_ext(middleFrame, height=sq, width=sq, bg="rosy brown", string="6")
bottom_leftFrame = Frame_ext(bottomFrame, height=sq, width=sq, bg="plum", string="7")
bottom_middleFrame = Frame_ext(bottomFrame, height=sq, width=sq, bg="violet", string="8")
bottom_rightFrame = Frame_ext(bottomFrame, height=sq, width=sq, bg="purple", string="9")
topFrame.pack(side=tk.TOP)
middleFrame.pack(side=tk.TOP)
bottomFrame.pack(side=tk.TOP)
top_leftFrame.pack(side=tk.LEFT)
top_middleFrame.pack(side=tk.LEFT)
top_rightFrame.pack(side=tk.LEFT)
middle_leftFrame.pack(side=tk.LEFT)
middle_middleFrame.pack(side=tk.LEFT)
middle_rightFrame.pack(side=tk.LEFT)
bottom_leftFrame.pack(side=tk.LEFT)
bottom_middleFrame.pack(side=tk.LEFT)
bottom_rightFrame.pack(side=tk.LEFT)
win.bind("<Button-1>", click)
win.mainloop()
有人知道这是什么原因吗?我不确定 class 继承是否正确。 对我来说看起来不错。谢谢
您需要将收到的 master
传递给超类 __init__
。此外,您不需要接受或传递 cnf
.
class Frame_ext(tk.Frame):
def __init__(self, master=None, string=None, **kw):
super().__init__(master=master, **kw)
^^^^^^