Python Tkinter 按钮命令在运行程序时激活?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3704568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:20:59  来源:igfitidea点击:

Tkinter button command activates upon running program?

pythonuser-interfacetkinter

提问by Doktor J

I'm trying to make a build retrieval form, and seem to have issues with the buttons... I'm a novice at Python/tkinter GUI programming (and GUI programming in general) and borrowed the skeleton of a Hello World app, and sorta built off that.

我正在尝试制作一个构建检索表单,但按钮似乎有问题......我是 Python/tkinter GUI 编程(以及一般的 GUI 编程)的新手,并借用了 Hello World 应用程序的框架,有点建立在这一点上。

In the code below, I've set the "command" option of my Browse button to call my class's internal get_dir() function when it's clicked. However, as soon as I attempt to run the app, the get_dir() function is called and I'm prompted to choose a directory. Any ideas why this happens, and what I can do to make it behave properly?

在下面的代码中,我设置了浏览按钮的“命令”选项,以便在单击时调用类的内部 get_dir() 函数。但是,一旦我尝试运行该应用程序,就会调用 get_dir() 函数并提示我选择一个目录。任何想法为什么会发生这种情况,以及我可以做些什么来使其行为正常?

from Tkinter import *
import tkFont
from tkFileDialog import askdirectory

class App:

    def __init__(self, master):

        fontHead = tkFont.Font(family="Arial", size=10, weight=tkFont.BOLD)
        fontBold = tkFont.Font(family="Arial", size=8, weight=tkFont.BOLD)
        fontReg =  tkFont.Font(family="Arial", size=8)

        frameN = Frame(master)
        frameN.grid(row=0,padx=5,pady=5)

        frameXBH = Frame(frameN)
        frameXBH.grid(row=0,columnspan=5,padx=5)

        Canvas(frameXBH,borderwidth=0,relief="flat",height=1,width=20,background="#cccccc").grid(row=0)
        Label(frameXBH, text="Xbox 360",font=fontBold,width=9).grid(row=0,column=1)
        Canvas(frameXBH,borderwidth=0,relief="flat",height=1,width=440,background="#cccccc").grid(row=0,column=2,sticky="WE")

        Label(frameN, text="Destination Path:",font=fontReg).grid(row=1,sticky="W")
        xbPath = Entry(frameN,width=30,font=fontReg)
        xbPath.grid(row=1,column=1,sticky="W")
        xbBrowse = Button(frameN,text="Browse...",font=fontReg,command=self.get_dir(xbPath))
        xbBrowse.grid(row=1,column=2,sticky="W")
        xbRel = Checkbutton(frameN,text="Release",font=fontReg)
        xbRel.grid(row=1,column=3,sticky="W")
        xbShip = Checkbutton(frameN,text="Ship",font=fontReg)
        xbShip.grid(row=1,column=4,sticky="W")

        Canvas(frameN,borderwidth=1,relief="groove",width=550,height=0).grid(row=2,columnspan=5,pady=10)

        # SAVE AND CANCEL

        btnSave = Button(frameN,text="Save",width=10)
        btnSave.grid(row=3,column=3,sticky="E")

        btnCancel = Button(frameN,text="Cancel",width=10)
        btnCancel.grid(row=3,column=4,sticky="W")

    def get_dir(self,box):
        tmp = askdirectory(mustexist=1,title="Please select a destination")
        tmp = tmp.replace("/","\")
        box.delete(0,END)
        box.insert(0,tmp)

root = Tk()
root.resizable(0,0)

app = App(root)

root.mainloop()

采纳答案by Kendall

Make your event handler a lambda function, which calls your get_dir()with whatever arguments you want:

使您的事件处理程序成为lambda 函数,它get_dir()使用您想要的任何参数调用您:

xbBrowse = Button(frameN, text="Browse...", font=fontReg, command=lambda : self.get_dir(xbPath))

回答by pyfunc

In the above code:

在上面的代码中:

xbBrowse = Button(frameN,text="Browse...",font=fontReg,command=self.get_dir(xbPath))

You are invoking the function already, you should be simply passing the function:

您已经在调用该函数,您应该简单地传递该函数:

xbBrowse = Button(frameN,text="Browse...",font=fontReg,command=self.get_dir)

回答by volting

You need to pass a reference of your get_dirmethod

您需要传递您的get_dir方法的引用

so change

所以改变

xbBrowse = Button(frameN,text="Browse...",font=fontReg,command=self.get_dir(xbPath))

to

xbBrowse = Button(frameN,text="Browse...",font=fontReg, command=self.get_dir)

Then make your Entrywidget an instance variable so that you can access it in your get_dirmethod.

然后让您的Entry小部件成为一个实例变量,以便您可以在您的get_dir方法中访问它。

e.g.

例如

self.xbPath = Entry(frameN,width=30,font=fontReg)

Then your get_dir()method will look like:

然后你的get_dir()方法看起来像:

def get_dir(self):
    tmp = askdirectory(mustexist=1,title="Please select a destination")
    tmp = tmp.replace("/","\")

    self.xbPath.delete(0,END)
    self.xbPath.insert(0,tmp)