Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python tkinter未连接到while循环/if语句_Python_Loops_While Loop_Tkinter - Fatal编程技术网

Python tkinter未连接到while循环/if语句

Python tkinter未连接到while循环/if语句,python,loops,while-loop,tkinter,Python,Loops,While Loop,Tkinter,我已经创建了一个程序,基本上验证了一个汽车注册号,执行了一个速度计算,然后将用户的详细信息写入记事本。如果用户的注册号无效或违反了速度限制,它工作正常。现在,我尝试使用tkinter使它更具交互性,但由于某些原因,它没有连接到while循环 tkinter计划: import tkinter from tkinter import * import random from random import randint import re def show_entry_fields(): p

我已经创建了一个程序,基本上验证了一个汽车注册号,执行了一个速度计算,然后将用户的详细信息写入记事本。如果用户的注册号无效或违反了速度限制,它工作正常。现在,我尝试使用tkinter使它更具交互性,但由于某些原因,它没有连接到while循环

tkinter计划:

import tkinter
from tkinter import *
import random
from random import randint
import re

def show_entry_fields():
   print("First Name: %s\nLast Name: %s\nRegistration number:%s" %        (Fname.get(), Lname.get(), reg.get()))

momma = Tk()
Label(momma, text="First Name").grid(row=0)
Label(momma, text="Last Name").grid(row=1)
Label(momma, text="please enter your registration number:").grid(row=2)

Fname = Entry(momma)
Lname = Entry(momma)
reg = Entry(momma)

Fname.grid(row=0, column=1)#colim = Insert the widget at this column.
Lname.grid(row=1, column=1)
reg.grid(row=2, column=1)

Button(momma, text='Quit', command=momma.quit).grid(row=3, column=0,               sticky=W, pady=4) 
Button(momma, text='Show', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)

mainloop()



while reg== re.match('^[A-Z]{2}[0-9]{2}[A-Z]{3}$', reg):
   w= Message(momma, text="that is a valid registration number")
   w.pack()
   break

我真的很想得到一些关于连接程序开始和while循环的帮助。提前感谢。

您的while循环只有在主循环完成之后,也就是Tk窗口关闭之后,才会进入。您要做的是在按下按钮时执行正则表达式,该按钮位于
show\u entry\u fields
函数中。虽然你的表达也没有多大意义,但它永远不会是真的:

from tkinter import *
import re

def show_entry_fields():
    print("First Name: %s\nLast Name: %s\nRegistration number:%s" % (Fname.get(), Lname.get(), reg.get()))
    if re.match('^[A-Z]{2}[0-9]{2}[A-Z]{3}$', reg.get()):
        w.configure(text='That is a valid registration number')
        # Do what you want to do after a valid registration number is entered, close the window, call a new function, whatever you want
    else:
        w.configure(text='Invalid registration number')

momma = Tk()
Label(momma, text="First Name").grid(row=0)
Label(momma, text="Last Name").grid(row=1)
Label(momma, text="please enter your registration number:").grid(row=2)
w= Label(momma, text="")

Fname = Entry(momma)
Lname = Entry(momma)
reg = Entry(momma)

Fname.grid(row=0, column=1)#colim = Insert the widget at this column.
Lname.grid(row=1, column=1)
reg.grid(row=2, column=1)
w.grid(row=4, column=0, columnspan=2, sticky=W+E)

Button(momma, text='Quit', command=momma.quit).grid(row=3, column=0,sticky=W, pady=4) 
Button(momma, text='Show', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)

momma.mainloop()