简体   繁体   English

从python的GUI中获取整数和字符串输入

[英]Taking integer and string input from GUI in python

I'm looking for a library to take an integer and string input from a user in Python. 我正在寻找一个库来接受Python中用户的整数和字符串输入。 Some Google searches point to using Tkinter and 'Tkinter Entry', but now how do I code it for integer and code another for string? Google的某些搜索指向使用Tkinter和“ Tkinter Entry”,但是现在我该如何为整数编码和为字符串编码另一个?

I'm trying to create a simple input box, take an integer as input and assign that integer to some variable. 我正在尝试创建一个简单的输入框,以整数作为输入并将该整数分配给某个变量。

if you want an actual GUI, then tkinter is the simplest way, but if you only want simple raw input then input() works fine. 如果您想要一个实际的GUI,则tkinter是最简单的方法,但是如果您只想要简单的原始输入,则input()可以正常工作。

example: 例:

test=input('prompt:') #take input
print(test*10) #print it ten times

simpledialog comes with python 3.x and has pop-ups that have an input box on them. simpledialog随python 3.x一起提供,并具有在其上带有输入框的弹出窗口。 but isn't as easy as input() 但是不像input()那样简单

The following code provides examples using tkinter with an Entry widget to get the user input or alternatively using tkinter.simpledialog to display a window for the user to enter a value. 以下代码提供了一些示例,这些示例将tkinter与Entry小部件一起使用来获取用户输入,或者使用tkinter.simpledialog显示一个供用户输入值的窗口。

Here is a useful guide to using tkinter . 这是使用tkinter的有用指南 There are many more out there for beginners like yourself 还有更多适合像您这样的初学者的产品

Code: 码:

import tkinter as tk
from tkinter.simpledialog import askstring, askinteger
from tkinter.messagebox import showerror


def display_1():
    # .get is used to obtain the current value
    # of entry_1 widget (This is always a string)
    print(entry_1.get())

def display_2():
    num = entry_2.get()
    # Try convert a str to int
    # If unable eg. int('hello') or int('5.5')
    # then show an error.
    try:
       num = int(num)
    # ValueError is the type of error expected from this conversion
    except ValueError:
        #Display Error Window (Title, Prompt)
        showerror('Non-Int Error', 'Please enter an integer')
    else:
        print(num)

def display_3():
    # Ask String Window (Title, Prompt)
    # Returned value is a string
    ans = askstring('Enter String', 'Please enter any set of characters')
    # If the user clicks cancel, None is returned
    # .strip is used to ensure the user doesn't
    # enter only spaces ' '
    if ans is not None and ans.strip():
        print(ans)
    elif ans is not None:
        showerror('Invalid String', 'You must enter something')

def display_4():
    # Ask Integer Window (Title, Prompt)
    # Returned value is an int
    ans = askinteger('Enter Integer', 'Please enter an integer')
    # If the user clicks cancel, None is returned
    if ans is not None:
        print(ans)

# Create the main window
root = tk.Tk()

# Create the widgets
entry_1 = tk.Entry(root)
btn_1 = tk.Button(root, text = "Display Text", command = display_1)

entry_2 = tk.Entry(root)
btn_2 = tk.Button(root, text = "Display Integer", command = display_2)

btn_3 = tk.Button(root, text = "Enter String", command = display_3)
btn_4 = tk.Button(root, text = "Enter Integer", command = display_4)

# Grid is used to add the widgets to root
# Alternatives are Pack and Place
entry_1.grid(row = 0, column = 0)
btn_1.grid(row = 1, column = 0)
entry_2.grid(row = 0, column = 1)
btn_2.grid(row = 1, column = 1)

btn_3.grid(row = 2, column = 0)
btn_4.grid(row = 2, column = 1)

root.mainloop()

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

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