Python 从其他模块中添加或删除tkinter小部件
我想知道如何在导入的模块中添加或删除小部件。我无法正确访问它们。我知道,使用OOP会更容易,但我试图掌握OOP,虽然原理很简单,但我无法理解细节,因此,由于我缺乏合适的老师,我需要一个程序性的解决方案 这是主脚本:Python 从其他模块中添加或删除tkinter小部件,python,module,tkinter,widget,Python,Module,Tkinter,Widget,我想知道如何在导入的模块中添加或删除小部件。我无法正确访问它们。我知道,使用OOP会更容易,但我试图掌握OOP,虽然原理很简单,但我无法理解细节,因此,由于我缺乏合适的老师,我需要一个程序性的解决方案 这是主脚本: #!/usr/bin/python try: # Python2 import Tkinter as tk except ImportError: # Python3 import tkinter as tk import os import sys
#!/usr/bin/python
try:
# Python2
import Tkinter as tk
except ImportError:
# Python3
import tkinter as tk
import os
import sys
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
import target
def myfunction(event):
canvas.configure(scrollregion=canvas.bbox("all"),width=300,height=200)
def test():
target.secondWindow()
root = tk.Tk()
root.geometry("600x350+30+50")
myframe = tk.Frame(root,relief="groove",bd=1)
myframe.place(x=20, y=30, width=560, height=200 )
canvas = tk.Canvas(myframe)
frame = tk.Frame(canvas)
myscrollbar=tk.Scrollbar(myframe, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=myscrollbar.set)
myscrollbar.pack(side="right", fill="y")
canvas.pack(side="left")
canvas.create_window((0,0), window=frame, anchor='nw')
allMissions = {
"1":{"name":"go"},
"2":{"name":"see"},
"3":{"name":"win"},
"4":{"name":"party"}} # this would be a text file
for a in allMissions.keys():
mn = allMissions[a]["name"]
tk.Label(frame, text=mn, justify="left").grid(row=int(a), column=0)
# what's bind really doing?
frame.bind("<Configure>", myfunction)
test = tk.Button(root, command=test, text="TEST")
test.place(x = 20, y = 250, width=580, height=40)
tk.mainloop()
您可以将任何小部件的内存地址传递给第二个程序。没有理由再次导入Tkinter,因为您可以只传递一个指向现有实例的指针。如果你要做的不仅仅是简单的Tkinter实验,那么花时间先在像这里这样的在线网站上学习课程是非常值得的 你不会从程序的结构中得到很多答案,因为大多数程序员都使用AFAIK类结构,所以不知道如何将代码放入非类环境中,因此不会有任何答案。如果下面的第一个程序使用了类,那么第二个程序的类可以被继承,函数将成为第一个程序类的一部分,并且可以以与现有类相同的方式访问,因此不需要传递指针或任何其他黑客行为
## I deleted some code for simplicity
def myfunction(event):
canvas.configure(scrollregion=canvas.bbox("all"),width=300,height=200)
def test():
TG.secondWindow()
root = tk.Tk()
root.geometry("600x350+30+50")
myframe = tk.Frame(root,relief="groove",bd=1)
myframe.place(x=20, y=30, width=560, height=200 )
canvas = tk.Canvas(myframe)
frame = tk.Frame(canvas)
myscrollbar=tk.Scrollbar(myframe, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=myscrollbar.set)
myscrollbar.pack(side="right", fill="y")
canvas.pack(side="left")
canvas.create_window((0,0), window=frame, anchor='nw')
# what's bind really doing?
frame.bind("<Configure>", myfunction)
test = tk.Button(root, command=test, text="TEST", bg="lightblue")
test.place(x = 20, y = 250, width=580, height=40)
tk.Button(root, text="Quit All", command=root.quit,
bg="orange").place(x=20, y=300)
""" instance of the class in the imported program
a pointer to the root window and the Tk instance are passed
"""
TG=target.Target(tk, root)
tk.mainloop()
您的第二个模块有问题-它不应该调用mainloop。谢谢,您是对的。我更正了。非常感谢您迄今为止的努力。我得到的代码工作,但改变的小部件是在子窗口。我如何将小部件添加到主窗口,尤其是显示的列表?我非常希望批准您的回答。因此,如果您可以从target.py,I will.hm中更改主窗口中的一个小部件,那么诀窍就是首先创建一组对象,而不使用init方法以外的任何其他方法,并且其中的所有方法都有引用?哇,这可能会打破一点僵局。凉的
## I deleted some code for simplicity
def myfunction(event):
canvas.configure(scrollregion=canvas.bbox("all"),width=300,height=200)
def test():
TG.secondWindow()
root = tk.Tk()
root.geometry("600x350+30+50")
myframe = tk.Frame(root,relief="groove",bd=1)
myframe.place(x=20, y=30, width=560, height=200 )
canvas = tk.Canvas(myframe)
frame = tk.Frame(canvas)
myscrollbar=tk.Scrollbar(myframe, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=myscrollbar.set)
myscrollbar.pack(side="right", fill="y")
canvas.pack(side="left")
canvas.create_window((0,0), window=frame, anchor='nw')
# what's bind really doing?
frame.bind("<Configure>", myfunction)
test = tk.Button(root, command=test, text="TEST", bg="lightblue")
test.place(x = 20, y = 250, width=580, height=40)
tk.Button(root, text="Quit All", command=root.quit,
bg="orange").place(x=20, y=300)
""" instance of the class in the imported program
a pointer to the root window and the Tk instance are passed
"""
TG=target.Target(tk, root)
tk.mainloop()
class Target():
def __init__(self, tk, root):
self.tk=tk
self.root=root
def changeMainWindow(self):
# here's where I'm stuck
self.tk.Label(self.amWin, bg="yellow", text =""""What do I have to do to add a new
label in the main window from here?
Or to delete it?""").place(x=50,y=20)
def secondWindow(self):
self.amWin = self.tk.Toplevel(self.root)
self.amWin.geometry("300x200+720+50")
button = self.tk.Button(self.amWin, text="Add Label",
command=self.changeMainWindow)
button.place(x = 20, y = 90, width=260, height=30).