Python 如何在tkinter环境中使用验证? 从tkinter导入* window=Tk() 窗口名称(“注册”) window.configure(background=“blue”)在此处输入代码 标签(窗口,text=“Firstname:”,bg=“blue”,fg=“white”,font=“verdana 12 bold”).grid(行=0,粘性=E) firstname=条目(窗口,宽度=100,bg=“白色”) grid(行=0,列=1,粘性=W) firstname=firstname.get() firstname=firstname.strip() firstname=firstname.lower() 标签(窗口,bg=“蓝色”).grid(行=2) 标签(窗口,text=“姓氏:”,bg=“蓝色”,fg=“白色”,font=“verdana 12粗体”).grid(行=3,粘滞=E) 姓氏=条目(窗口,宽度=100,bg=“白色”) 姓氏网格(行=3,列=1,粘性=W) 姓氏=姓氏。get() 姓氏=姓氏。下() 姓氏=姓氏.strip() 标签(窗口,bg=“蓝色”).grid(行=4) 标签(窗口,text=“选择一个介于0和10之间的数字:”,bg=“蓝色”,fg=“白色”,font=“verdana 12粗体”)。网格(行=5,粘性=E) 数字=输入(窗口,宽度=100,bg=“白色”) 数字网格(行=5,列=1) 尽管如此: 尝试: number=number.get() 如果(数字>10)或(数字
这是到目前为止我的代码。我正在尝试为我正在进行的测验创建一个注册系统,但是,现在我正在处理GUI,我不知道如何在GUI方式/环境中使用我的验证代码。例如,仅使用“打印”(“对不起,您的响应必须是介于0和10之间的数字”)不会也不适用于我的程序。 我的问题:如何将消息像错误消息一样输出到文本框中,以及如何实现验证代码? 另外,我在几月前刚接触python时编写了验证代码,并使用了一段堆栈溢出代码来帮助将其应用到我的程序中。不管怎样,有人能解释一下这段代码是如何工作的吗。我现在似乎不明白,我的老师一直在努力以一种可以理解的方式解释它。特别是:1/0位。我不习惯使用try和except,我通常只知道如何使用for和while循环。 非常感谢在tkinter中显示信息(第一个问题) 要显示用户基本信息并获取基本选项(显示错误,请询问“确定/取消”或“是/否…”),您可以使用Python 如何在tkinter环境中使用验证? 从tkinter导入* window=Tk() 窗口名称(“注册”) window.configure(background=“blue”)在此处输入代码 标签(窗口,text=“Firstname:”,bg=“blue”,fg=“white”,font=“verdana 12 bold”).grid(行=0,粘性=E) firstname=条目(窗口,宽度=100,bg=“白色”) grid(行=0,列=1,粘性=W) firstname=firstname.get() firstname=firstname.strip() firstname=firstname.lower() 标签(窗口,bg=“蓝色”).grid(行=2) 标签(窗口,text=“姓氏:”,bg=“蓝色”,fg=“白色”,font=“verdana 12粗体”).grid(行=3,粘滞=E) 姓氏=条目(窗口,宽度=100,bg=“白色”) 姓氏网格(行=3,列=1,粘性=W) 姓氏=姓氏。get() 姓氏=姓氏。下() 姓氏=姓氏.strip() 标签(窗口,bg=“蓝色”).grid(行=4) 标签(窗口,text=“选择一个介于0和10之间的数字:”,bg=“蓝色”,fg=“白色”,font=“verdana 12粗体”)。网格(行=5,粘性=E) 数字=输入(窗口,宽度=100,bg=“白色”) 数字网格(行=5,列=1) 尽管如此: 尝试: number=number.get() 如果(数字>10)或(数字,python,tkinter,computer-science,registration,user-registration,Python,Tkinter,Computer Science,Registration,User Registration,这是到目前为止我的代码。我正在尝试为我正在进行的测验创建一个注册系统,但是,现在我正在处理GUI,我不知道如何在GUI方式/环境中使用我的验证代码。例如,仅使用“打印”(“对不起,您的响应必须是介于0和10之间的数字”)不会也不适用于我的程序。 我的问题:如何将消息像错误消息一样输出到文本框中,以及如何实现验证代码? 另外,我在几月前刚接触python时编写了验证代码,并使用了一段堆栈溢出代码来帮助将其应用到我的程序中。不管怎样,有人能解释一下这段代码是如何工作的吗。我现在似乎不明白,我的老师一
tkinter.messagebox
。它提供了show*()
和ask*()
功能。在您的情况下,showerror()
或showwarning()
可能是最好的选择
要获取基本输入,可以使用tkinter.simpledialog
。它提供了函数askinteger
、askfloat
和askstring
,这些函数将向用户询问各自的数据类型
要获取文件(路径)输入,请使用tkinter.filadialog
对于更复杂的情况,最好使用小部件
第二项问题
你的代码
我将扮演解释器并检查您的代码。如果您只是想要解决方案(不推荐),请跳到下面
from tkinter import *
window = Tk()
window.title("Registration")
window.configure(background="blue")enter code here
Label (window, text= "Firstname: ", bg="blue", fg="white", font="verdana 12 bold") .grid(row=0, sticky=E)
firstname = Entry(window, width=100, bg="white")
firstname.grid(row=0, column=1, sticky=W)
firstname = firstname.get()
firstname = firstname.strip()
firstname = firstname.lower()
Label (window, bg = "blue") .grid(row=2)
Label (window, text= "Surname: ", bg="blue", fg="white", font="verdana 12 bold") .grid(row=3, sticky=E)
surname = Entry(window, width=100, bg="white")
surname.grid(row=3, column=1, sticky=W)
surname = surname.get()
surname = surname.lower()
surname = surname.strip()
Label (window, bg = "blue") .grid(row=4)
Label (window, text = "Pick a number between 0 and 10: ", bg="blue", fg="white", font = "verdana 12 bold") .grid(row=5, sticky=E)
number = Entry(window, width=100, bg="white")
number.grid(row=5, column=1)
while True:
try:
number = number.get()
if (number > 10) or (number < 0): 1/0
except:
print("Sorry, your response must be a number between 0 and 10")
continue
break
window.mainloop()
好的,你想要得到文本。只是,窗户还不可见。这些指令将在空闲shell中工作,因为您将等待调用.get()
,直到键入您的名称。在“真实”执行中,解释器不会等待,用户也不能在调用.mainloop()
之前键入(因为没有窗口)。如果您阅读以上内容,一种解决方案是使用simpledialog
。但这也应该在GUI启动后运行,即调用.mainloop()
后。我以后再谈那部分
--同姓--
你的验证
口译员:
firstname = Entry(...) => create an Entry widget and assign it to firstname
firstname.grid(...) => put the widget in/on the window
firstname = firstname.get() => get the text currently in the widget and assign it to firstname.
while True:
try:
value = int(input())
if not 0<value<10:
1/0
except ZeroDivisionError: # let ValueError through (you willl want to catch it separately)
print('by executing 1/0, I raised a ZeroDivisionError. This code therefore will execute.')
else:
print('Everything did fine. I will exit the loop to continue.')
break
你会陷入一个无限的异常循环,即使用户输入了正确的数字也不会停止(这无论如何都不会发生,我们还没有一个窗口)。这是一个非常好的理由来避免光秃秃的,除了
——你也会抓到很多你不想要的东西
为什么你尝试使用Woooould工作的方法(你说你在这里找到了它——你还记得链接或标题吗?)
代码(为了简单起见,此示例位于控制台中):
你不能用零除法!firstname变量应该是条目对象,但可以使用get()函数将其覆盖为。出于这个目的,最好使用tkinters StringVar,您的姓氏变量也是如此。您的if语句是错误的,因为您询问的数字是否大于10且小于0,但您希望捕获一个介于0和10之间的数字。
while True:
try:
value = int(input())
if not 0<value<10:
1/0
except ZeroDivisionError: # let ValueError through (you willl want to catch it separately)
print('by executing 1/0, I raised a ZeroDivisionError. This code therefore will execute.')
else:
print('Everything did fine. I will exit the loop to continue.')
break
# loop
# in try
value = int(input()) => read input and try to convert to an int; we assume this doesn't fail.
# if condition
not 0<value<10 => is value not between 0 and 10? (we assume it isn't)
# if block
1/0 => what?!? I can't do that, I'll raise a ZeroDivisionError
# except block
print(...) => show the text
# loop again
# in try
value = int(input()) => as above
# if condition
not 0<value<10 => we assume value is between 0 and 10 (evaluetes to False)
# else part of the try...except...else
print(...) => show text
break => exit the loop
import tkinter as tk
from tkinter.simpledialog import askstring, askinteger
from tkinter.messagebox import showwarning
def do_stuff(first_name, surname, number):
...
def start():
# if you want to use Entry, add a "Submit" Button
first_name = askstring('Title', 'first name:')
surname = askstring('Title', 'last name:')
while True: # ask* return None when cancel is pressed
number = askinteger('Title', 'insert a number between 0 and 10:')
if number is not None and 0<number<10: # what we want
break
do_stuff(first_name, surname, number)
# GUI preparation code
window = tk.Tk()
button_start = tk.Button(window, text='Start!', command=start)
button_start.pack() # for use with other widgets (that use grid), you must .grid() here
window.mainloop() # the GUI appears at this moment