【问题标题】:Why does the "command" directly open and not when clicked on Button? Python 3为什么“命令”直接打开而不是单击按钮时?蟒蛇 3
【发布时间】:2013-10-02 16:04:02
【问题描述】:

我在这整个 Python 方面还很新,我的问题是如何让一个按钮在单击它时而不是之前运行命令。
我在互联网上搜索了很多,但我没有找到任何东西。 我根本听不懂这些课程。有没有其他方法可以做到这一点?

这是我的工作,我是在程序上完成的。 感谢您的帮助

from tkinter import *
import os
t = ""

def ordner(x):
    print ("def")
    if os.path.exists(os.path.join("/Kunden/",x)) == True:
        pass
    else:
        os.mkdir(os.path.join("/Kunden/",x))

def E1holen():
    x = E1.get()
    ordner(x)


#Hauptfenster
main=Tk(className='Kundendatenbank')
main.iconbitmap('icon.ico')
#Inhalt Hauptfenster
L1 = Label(main, text="Kundenname:")
L1.pack(side = LEFT)
E1 = Entry(main, bd =5, textvariable=t)
E1.pack(side = RIGHT)
a = Button (main, text=("erstellen/bearbeiten"), command=E1holen()).pack()

main.mainloop()

【问题讨论】:

    标签: button tkinter command


    【解决方案1】:

    它会立即运行,因为你告诉它。

    在 Python 中调用函数的语法是什么?是foo(),对吧?那么,当你做command=E1holen()时,python应该怎么做呢?它应该调用E1holen(),然后将结果传递给command 属性。

    换句话说,command 属性将 reference 指向一个函数,但由于 () 您正在 调用该函数并给出 @987654327 @ 属性无论该函数返回什么。解决方案?删除():

    a = Button(..., command=E1holen)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-19
      • 2021-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      相关资源
      最近更新 更多