【问题标题】:Adding or deleting tkinter widgets from within other modules从其他模块中添加或删除 tkinter 小部件
【发布时间】:2014-11-19 14:46:40
【问题描述】:

我想知道如何在导入的模块中添加或删除小部件。我无法正确访问它们。我知道,使用 OOP 会更容易,但我试图掌握 OOP,虽然原理很简单,但我无法理解细节,所以由于我缺乏合适的老师,我需要一个程序解决方案。

这是主脚本:

#!/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()

这是导入的模块:target.py

try:
   # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk  

def changeMainWindow():
   # here's where I'm stuck
   print("What do I have to do to add a new") 
   print("label in the main window from here?")
   print("Or to delete it?")  


def secondWindow():


    amWin = tk.Toplevel()

    amWin.geometry("300x200+720+50")

    button = tk.Button(amWin, text="OK", command=changeMainWindow)
    button.place(x = 20, y = 80, width=260, height=30) 

    #amWin.mainloop() comment noticed (:

【问题讨论】:

  • 你的第二个模块有问题——它不应该调用mainloop
  • 谢谢你,你是对的。我更正了。

标签: python module tkinter widget


【解决方案1】:

您可以通过将任何小部件的内存地址传递给第二个程序来实现。没有理由再次导入 Tkinter,因为您只需将指针传递给现有实例。如果您要做的不仅仅是使用 Tkinter 进行简单的实验,那么值得花时间在像这样的在线网站之一上学习课程http://www.greenteapress.com/thinkpython/html/thinkpython016.html更多https://wiki.python.org/moin/BeginnersGuide/NonProgrammers
对于程序的结构方式,您不会得到很多答案,因为大多数程序员使用类结构 AFAIK,所以不知道如何将代码放入非类环境中,所以不会有任何答案。如果下面的第一个程序使用类,则可以继承第二个程序的类,并且这些函数将成为第一个程序类的一部分,并且可以以与现有类相同的方式访问,因此无需传递指针或任何其他 hack , 是必要的。

## 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()

还有target.py。注意没有导入。

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).

【讨论】:

  • 非常感谢您迄今为止所做的努力。我让代码可以工作,但更改后的小部件位于子窗口中。如何将小部件添加到主窗口,尤其是显示的列表?
  • 我非常愿意批准您的回答。所以,如果你可以在 target.py 中更改主窗口中的小部件,我会的。
  • hm,所以诀窍是首先创建一堆对象而不使用init方法以外的任何其他方法,并且里面的所有方法都有引用?哇,这可能会打破僵局。酷
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 2012-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多