Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 所有tkinter函数在程序启动时运行_Python_Function_Tkinter - Fatal编程技术网

Python 所有tkinter函数在程序启动时运行

Python 所有tkinter函数在程序启动时运行,python,function,tkinter,Python,Function,Tkinter,我在使用tkinter时遇到了一个前所未有的奇怪问题。无论我在哪里为小部件(如按钮或菜单项)设置命令,该命令都会在应用程序启动时运行。基本上,该命令不会等到小部件被单击后才运行。在我的代码中,我知道我没有打包这个按钮,这是为了显示这个小部件甚至不必被绘制到屏幕上,这个问题就会发生。有人知道是什么引起的吗?谢谢 from tkinter import * class menuItems(object): def __init__(self): menubar = Menu

我在使用tkinter时遇到了一个前所未有的奇怪问题。无论我在哪里为小部件(如按钮或菜单项)设置命令,该命令都会在应用程序启动时运行。基本上,该命令不会等到小部件被单击后才运行。在我的代码中,我知道我没有打包这个按钮,这是为了显示这个小部件甚至不必被绘制到屏幕上,这个问题就会发生。有人知道是什么引起的吗?谢谢

from tkinter import *

class menuItems(object):
    def __init__(self):
        menubar = Menu(app)
        filemenu = Menu(menubar, tearoff=0)
        filemenu.add_command(label="New...", command=self.new())
        filemenu.add_command(label="Open...", command=self.open())
        filemenu.add_command(label="Save", command=self.save())
        filemenu.add_separator()
        filemenu.add_command(label="Exit", command=app.quit)
        menubar.add_cascade(label="File", menu=filemenu)
        app.config(menu=menubar)

    def new(self):
        pass

    def open(self):
        pass

    def save(self):
        print("You have saved the file")

def this_should_not_run():
    print("Yay! I didn't run!")

def this_will_run_even_though_it_should_not():
    print("You can't stop me!")

def init():
    global app, menu
    app = Tk()
    app.title("Words with Python")
    app.geometry("800x500+50+50")

    menu = menuItems()

    frame = Frame(app)
    scrollbar = Scrollbar(frame, orient=VERTICAL)
    textbox = Text(frame, yscrollcommand=scrollbar.set)
    scrollbar.config(command=textbox.yview)
    scrollbar.pack(side=RIGHT, fill=Y)
    textbox.pack(side=LEFT, fill=BOTH, expand=1)
    frame.pack(fill=BOTH, expand=1)

    button = Button(app, text="Nothing", command=this_will_run_even_though_it_should_not())

    return

init()

app.mainloop()
在这些行中,必须将引用传递给函数。实际上,您正在调用这些函数

filemenu.add_command(label="Open...", command=self.open)
filemenu.add_command(label="New...", command=self.new)
filemenu.add_command(label="Open...", command=self.open)
filemenu.add_command(label="Save", command=self.save)
删除命令定义中的
()
s。现在,您正在调用函数并将返回值绑定到
命令
参数,而您需要绑定函数本身,以便以后可以调用它们

这样一句话:

filemenu.add_command(label="New...", command=self.new())
实际上应该是这样的:

filemenu.add_command(label="New...", command=self.new)

(您实际上在一个位置正确地执行了此操作:
filemenu.add_命令(label=“Exit”,command=app.quit)

感谢您的回复!我没有意识到实际上调用了这个函数。问题解决了!
filemenu.add_command(label="New...", command=self.new)