简体   繁体   English

如何使用 Python tkinter 向用户询问整数输入

[英]How to ask user for integer input using Python tkinter

I need to ask a user for an integer value and tried a=n.get() and then a=int(a) but it doesn't work as expected.我需要向用户询问一个整数值并尝试a=n.get()然后a=int(a)但它没有按预期工作。 Here the code I used:这是我使用的代码:

def selectparamter():
    window3= Tk()
    window3.title('choosing parameters ')
    window3.resizable(False,False)
    a_label = Label(window3, text = 'Select  parameter', font=('calibre',10, 'bold'))
    n = Entry(window3)
    string_answer = n.get()
    a= int(string_answer)
    sub_btn=Button(window3,text = 'Submit', command =nrmltest )
    sub_btn.grid(row=2,column=1)
    a_label.grid(row=0,column=0)
    n.grid(row=0,column=1)
    window3.mainloop()
    return a

How can I get the right integer value input from user in Python tkinter?如何在 Python tkinter 中从用户那里获得正确的整数值输入?

def selectparamter():
    window3= Tk()
    window3.title('choosing parameters ')
    window3.resizable(False,False)
    a_label = Label(window3, text = 'Select  parameter', font=('calibre',10, 'bold'))
    n_var = IntVar()
    n = Entry(window3, textvariable=n_var)
    a = n_var.get()
    sub_btn=Button(window3,text = 'Submit', command =nrmltest )
    sub_btn.grid(row=2,column=1)
    a_label.grid(row=0,column=0)
    n.grid(row=0,column=1)
    window3.mainloop()
    return a

For this code, IntVar class of tkinter was used to give value to "textvariable" attribute of widget.对于此代码,使用 tkinter 的 IntVar 类为小部件的“textvariable”属性赋值。 For more info,press here.欲了解更多信息,请此处。 This can return the value of Entry widget as class but it will throw exception if string is given in the widget.这可以将 Entry 小部件的值作为类返回,但如果在小部件中给出字符串,它将抛出异常。 The value of 'a' seems to have been called right after widget creation. 'a' 的值似乎是在小部件创建后立即调用的。 so, now with no text in widget, it will not perform properly.因此,现在小部件中没有文本,它将无法正常执行。

Here you are ("If anybody knows how to solve this pls answer"):你在这里(“如果有人知道如何解决这个问题,请回答”):

from tkinter import Tk, Label, Entry, Button, IntVar 
def selectparameter():
    def getValueFromUser():
        window3.quit()
    window3= Tk()
    window3.title('Choosing Parameters ')
    window3.resizable(False, False)
    a_label = Label(window3, text = 'Provide an integer value:  ', font=('calibre',10, 'bold'))
    n_var   = IntVar()
    n_var.set('')
    n = Entry(window3, textvariable = n_var)
    n.focus_set()
    sub_btn=Button(window3, text='Submit', command = getValueFromUser )
    sub_btn.grid(row=2,column=1)
    a_label.grid(row=0,column=0)
    n.grid(row=0,column=1)
    window3.mainloop()
    a = n_var.get()
    return a
print(selectparameter())

BUT ... tkinter comes with a method which is both simpler and also better suited to get an integer input from user as it checks the user input and asks the user again in case of detected non-integer value:但是... tkinter 提供了一种更简单且更适合从用户获取整数输入的方法,因为它检查用户输入并在检测到非整数值的情况下再次询问用户:

from tkinter import Tk, simpledialog
root=Tk()
root.withdraw()
intUserInput = simpledialog.askinteger(title="askinteger", prompt="integerInputPlease")
root.destroy()
print(intUserInput)

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

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