Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

python - Tkinter button command activates upon running program?

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.

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?

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...