Python 如何将一个小部件插入另一个小部件,如果它们来自不同的类。以及如何创建关闭子窗口小部件的通用功能?
这是我的密码。我不能将label2放在self.main中,我也不知道如何编写一个通用函数代码,它将关闭可以在参数中指定的子窗口小部件Python 如何将一个小部件插入另一个小部件,如果它们来自不同的类。以及如何创建关闭子窗口小部件的通用功能?,python,class,tkinter,widget,Python,Class,Tkinter,Widget,这是我的密码。我不能将label2放在self.main中,我也不知道如何编写一个通用函数代码,它将关闭可以在参数中指定的子窗口小部件 import tkinter class mainwin: def __init__(self): self.root = tkinter.Tk() self.main = tkinter.Canvas(self.root, width=200, height=400) self.main.place(
import tkinter
class mainwin:
def __init__(self):
self.root = tkinter.Tk()
self.main = tkinter.Canvas(self.root, width=200, height=400)
self.main.place(x=0, y=0, relwidth=1, relheight=1)
self.main.config(bg='green')
self.root.mainloop()
class addlabel:
def __init__(self):
self.label2 = tkinter.Label(mainwin.main, height=2, width=50, text='Hello Noob!!')
#can't put on the canvas 'main'
self.label2.place(x=0, y=50)
self.exit_button = tkinter.Button(self.label2, text='Exit')
self.exit.button.bind('<1>', quit_from_widget)
'''
class quit_from_widget:
def __init__(self):
# what code should be written here, to quit any child widget.
'''
mainwin()
addlabel()
导入tkinter
mainwin类:
定义初始化(自):
self.root=tkinter.Tk()
self.main=tkinter.Canvas(self.root,宽度=200,高度=400)
self.main.place(x=0,y=0,relwidth=1,relheight=1)
self.main.config(bg='green')
self.root.mainloop()
类别addlabel:
定义初始化(自):
self.label2=tkinter.Label(mainwin.main,高度=2,宽度=50,text='Hello Noob!!')
#无法在画布上放置“main”
自标签2.位置(x=0,y=50)
self.exit_button=tkinter.button(self.label2,text='exit')
self.exit.button.bind(“”,退出\u小部件)
'''
类从\u小部件退出\u:
定义初始化(自):
#要退出任何子窗口小部件,这里应该编写什么代码。
'''
梅因温()
addlabel()
您可以使用:
mylist = parent.winfo_children();
然后使用for循环和destroy()关闭它们
无法将标签放入的主要原因是在addLabel之前调用mainloop()。程序在代码中循环,直到关闭mainwin()函数后才执行addlabel()
其次,你不能做mainw.main。该类没有对该函数的引用。请尝试向addlabel添加父函数,如下所示:
class addlabel:
def __init__(self, parent):
self.label2 = tkinter.Label(parent, height=2, width=50, text='Hello Noob!!')
self.label2.place(x=0, y=50)
self.exit_button = tkinter.Button(self.label2, text='Exit')
self.exit_button.bind('<1>', quit)
什么也没发生。提供绿色背景,子窗口小部件不可见。但在结束时,他写了一个错误:.... (widgetName,self._w)+额外+自选项(cnf)) _tkinter.TclError:无法调用“frame”命令:应用程序已被销毁
进程结束时退出代码1当您思考时,最后对
addlabel()
的调用不会执行,因为在mainwin中先前对self.root.mainloop()
的调用在关闭窗口之前不会返回。\uu init\uuuu()
。您可以通过我的代码编写示例,了解如何执行所有操作吗?我将非常感激。如果你能先尝试一下我的建议,我将不胜感激。哪一部分给你带来麻烦?这似乎不是问题的答案。
addlabel(self.main)
import tkinter
class mainwin:
def __init__(self):
self.root = tkinter.Tk()
self.main = tkinter.Canvas(self.root, width=200, height=400)
self.main.place(x=0, y=0, relwidth=1, relheight=1)
self.main.config(bg='green')
self.root.mainloop()
class CustomLabel(tkinter.Frame):
def __init__(self, parent):
tkinter.Frame.__init__(self, parent)
self.label = tkinter.Label(self, height=20, width=30, bg='Red', fg='white', text='Hello')
self.exit_button = tkinter.Button(self, command=self.destroy)
# pack these widgets into this frame. You can use grid or
# place, but pack is easiest for such a simple layout
self.exit_button.pack(side="right")
self.label.pack(side="left", fill="both", expand=True)
window = mainwin()
label = CustomLabel(window.main)