繁体   English   中英

Python Tkinter使用输入字段创建MessageBox

[英]Python Tkinter Use Entry Field to Create MessageBox

我是Tkinter的新手,并且具有很少的Python经验,所以我希望答案不太明显,我试图搜索答案,但找不到任何有用的方法。 从本质上讲,我正在尝试构建一个程序(如果作为占位符测试,现在),如果用户在输入字段中输入1并点击提交,则会出现一个窗口,告诉他们输入了1,否则要求输入1。如果我的理解正确,这应该起作用:

    from Tkinter import *
    #-----------------------------------------------------------
    import tkMessageBox
    root = Tk()
    #-----------------------------------------------------------
    root.title('Payroll System')
    #-----------------------------------------------------------
    def on_printtext(root):
        global entryform
        string = entryform.get()
        if string == 1:
            tkMessageBox.showinfo('You typed 1')
        elif string != 1:
            tkMessageBox.showinfo('Please type 1')
    #-----------------------------------------------------------
    entryform = Entry(root)
    entryform.pack()
    submit = Button(root, text="Submit", command=on_printtext)
    submit.pack()
    root.mainloop()

但是,当我尝试运行它并在单击“提交”后在输入表单中输入1时,我得到了:


    Exception in Tkinter callback
    Traceback (most recent call last):
      File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in call
        return self.func(*args)
    TypeError: on_printtext() takes exactly 1 argument (0 given)

问题是tkinter试图调用注册为按钮command的函数而没有任何参数,但是您的函数有1个参数root没有任何默认变量,因此这导致您遇到问题。

代码中还有其他一些问题-

  1. Entry.get()返回一个字符串,但是您试图将其与一个整数进行比较,它永远不会相等,因此即使您输入1,它仍然会显示Please type 1

  2. 这样做时tkMessageBox.showinfo('You typed 1') -您实际上是将title设置为You typed 1 ,而不是实际的消息。 对于tkMessageBox的功能, the first argument is the title, and the second argument is the message. If you want that as the message, set it as the , the first argument is the title, and the second argument is the message. If you want that as the message, set it as the请使用关键字参数, the first argument is the title, and the second argument is the message. If you want that as the message, set it as the message`。 范例-

     tkMessageBox.showinfo(message='You typed 1') 

有效的示例代码-

from Tkinter import *
import tkMessageBox

root = Tk()
root.title('Payroll System')
def on_printtext():
    global entryform
    strng = entryform.get()
    if strng == '1':
        tkMessageBox.showinfo(message='You typed 1')
    else:
        tkMessageBox.showinfo(message='Please type 1')

entryform = Entry(root)
entryform.pack()
submit = Button(root, text="Submit", command=on_printtext)
submit.pack()

root.mainloop()

如果您使用的是Python 3.x,则由于tkMessageBox已更改为messagebox,因此上面的代码不起作用。

这是修改后的代码:

from tkinter import * # modif 1 Tkinter with minus t !
import tkinter.messagebox # modif 2:tkMessageBox no longer valid

root = Tk()
root.title('Payroll System')
def on_printtext():
    global entryform
    strng = entryform.get()
    if strng == '1':
        tkinter.messagebox.showinfo(message='You typed 1') # modif 3
    else:
        tkinter.messagebox.showinfo(message='Please type 1') # modif 4

entryform = Entry(root)
entryform.pack()
submit = Button(root, text="Submit", command=on_printtext)
submit.pack()

root.mainloop() 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM