如何在Python中错误输入时显示弹出窗口?
我正在尝试编写一个tkinter gui来读取用户的输入。 如果输入不以“”开头https://www.youtube.com/watch?v=“然后会显示一个弹出窗口,通知用户链接无效,用户应该可以重试。在输入的url有效之前,应一直执行此操作。如果输入有效,将调用另一个函数(我还没有编写) 但是,当url无效时,弹出窗口显示,但它永远不会返回到输入,并始终显示弹出窗口,直到我终止程序 这是我的代码,非常感谢您的帮助:如何在Python中错误输入时显示弹出窗口?,python,tkinter,Python,Tkinter,我正在尝试编写一个tkinter gui来读取用户的输入。 如果输入不以“”开头https://www.youtube.com/watch?v=“然后会显示一个弹出窗口,通知用户链接无效,用户应该可以重试。在输入的url有效之前,应一直执行此操作。如果输入有效,将调用另一个函数(我还没有编写) 但是,当url无效时,弹出窗口显示,但它永远不会返回到输入,并始终显示弹出窗口,直到我终止程序 这是我的代码,非常感谢您的帮助: import tkinter as tk from tkinter imp
import tkinter as tk
from tkinter import messagebox
class App:
def __init__(self, root):
# setting title
root.title("YouTube Downloader")
# setting window size
width = 600
height = 500
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
root.geometry(alignstr)
root.resizable(width=False, height=False)
self.LineEdit = tk.Entry(root)
self.LineEdit["borderwidth"] = "2px"
self.LineEdit.place(x=250, y=50, width=289, height=30)
LineLabel = tk.Label(root)
LineLabel["text"] = "Enter YouTube URL to analyze:"
LineLabel.place(x=60, y=50, width=177, height=30)
GoButton = tk.Button(root)
GoButton["text"] = "Analyze"
GoButton.place(x=170, y=130, width=195, height=58)
GoButton["command"] = self.DisplayInput
def DisplayInput(self):
isvalid = False
while not isvalid:
result = self.LineEdit.get()
line = str(result).lower()
if not line.startswith("https://www.youtube.com/watch?v="):
messagebox.showerror("Error!", "The URL doesn't seems to be a valid YouTube URL.\n\nPlease try again.")
else:
isvalid = True
print(result)
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()
而不是在DisplayInput函数中使用while循环。只要使用if语句,每当用户输入的url不是以您想要的url开头时,就会打开错误消息框。这将仅在用户按analyze时打开错误消息框
import tkinter as tk
from tkinter import messagebox
class App:
def __init__(self, root):
# setting title
root.title("YouTube Downloader")
# setting window size
width = 600
height = 500
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / (screenheight - height) / 2)
root.geometry(alignstr)
root.resizable(width=False, height=False)
self.LineEdit = tk.Entry(root)
self.LineEdit["borderwidth"] = "2px"
self.LineEdit.place(x=250, y=50, width=289, height=30)
#I added this line
self.isvalid = False
LineLabel = tk.Label(root)
LineLabel["text"] = "Enter YouTube URL to analyze:"
LineLabel.place(x=60, y=50, width=177, height=30)
GoButton = tk.Button(root)
GoButton["text"] = "Analyze"
GoButton.place(x=170, y=130, width=195, height=58)
def DisplayInput():
# I changed this part
result = self.LineEdit.get()
line = str(result).lower()
if not line.startswith("https://www.youtube.com/watch?v="):
messagebox.showerror("Error!", "The URL doesn't seems to be a valid YouTube URL.\n\nPlease try again.")
else:
self.isvalid = True
print(result)
GoButton["command"] = DisplayInput
if __name__ == "__main__":
root = tk.Tk()
app = App(root)
root.mainloop()
更新:我设法解决了这个问题 我对“DisplayInput”功能做了一个小改动:
def DisplayInput(self):
result = self.LineEdit.get()
line = str(result).lower()
if not line.startswith("https://www.youtube.com/watch?v="):
messagebox.showerror("Error!", "The URL doesn't seems to be a valid YouTube URL.\n\nPlease try again.")
# I added the line below
root.mainloop()
print(result)
@谢谢你的回答。但是,我得到了以下错误:Tkinter回调回溯异常(最近一次调用是最后一次):在调用返回self.func(*args)文件“C:/Users/s/Google Drive/Studies/Python/My Games/YouTube Downloader/1.py”第41行的DisplayInput GoButton[“command”]=DisplayInput名称错误:名称“DisplayInput”未定义您运行的代码与我发布的代码完全相同吗?我运行了代码,没有收到任何错误。我复制/粘贴了。。。奇怪的是为什么它不起作用