[英]I encountered a problem while I was coding with the Python Tkinter module
Recently, I've encountered a problem while I'm coding with the Python Tkinter module.最近,我在使用 Python Tkinter 模块进行编码时遇到了一个问题。
The project is a URL shortener.该项目是 URL 缩短器。
Here is the code I wrote:这是我写的代码:
import tkinter as tk
import random
import string
def shorten_url():
url = url_input_space_entry.get()
result_url_random_part = ''.join(random.choice(string.hexdigits) for digit in range(6))
result_url = f"www.urlshort.com/{result_url_random_part}"
show_result.configure(text=result_url)
window = tk.Tk()
window.title("URL Shortener")
window.geometry("700x600")
app_title = tk.Label(window, text="URL Shortener")
app_title.pack()
url_input_space = tk.Frame(window)
url_input_space.pack()
url_input_label = tk.Label(url_input_space, text="Please enter your URL: ")
url_input_label.pack(side=tk.LEFT)
url_input_space_entry = tk.Entry(url_input_space, width=45)
url_input_space_entry.pack()
result_frame = tk.Frame(window)
result_frame.pack()
result_label = tk.Label(result_frame, text="Result: ")
result_label.pack(side=tk.LEFT)
show_result = tk.Entry(result_frame, width=34)
show_result.pack()
shorten_url_button = tk.Button(window, text="Shorten it", command=shorten_url)
shorten_url_button.pack()
window.mainloop()
So, I expect that:所以,我希望:
First, I enter the URL that needs to be shortened:首先我输入需要缩短的URL:
Please enter your URL: xxxx.com (Example URL)
请输入您的 URL:xxxx.com(示例 URL)
Then, after I pressed the 'Shorten it' button, this is the result I expect to get in the resulting frame:然后,在我按下“缩短”按钮后,这是我希望在结果帧中得到的结果:
Result: www.urlshort.com/XXXXXX (Random 6-digits number and alphabet eg 9eDS34, A1e2wS, etc.)
结果: www.urlshort.com/XXXXXX (随机 6 位数字和字母,例如 9eDS34、A1e2wS 等)
But now, the RESULT frame didn't show anything after I pressed the button.但是现在,我按下按钮后,RESULT 框架没有显示任何内容。 (The reason why I have no clue about the problem at all, is that after I ran the program, there is no error message came out.)
(我完全不知道这个问题的原因是我运行程序后没有出现错误消息。)
Can someone tell me what did I code wrong and how can I fix it?有人能告诉我我的代码有什么问题吗?我该如何解决? Thanks a lot!
非常感谢!
You can't use configure method with Entry widget.您不能将配置方法与 Entry 小部件一起使用。 You should instead clear the Entry, and then insert your string like this:
您应该清除条目,然后像这样插入您的字符串:
show_result.delete(0, tk.END)
show_result.insert(0,result_url)
Your main code should be你的主要代码应该是
def shorten_url():
url = url_input_space_entry.get()
result_url_random_part = ''.join(random.choice(string.hexdigits) for digit in range(6))
result_url = f"www.urlshort.com/{result_url_random_part}"
show_result.delete(0, tk.END)
show_result.insert(0,result_url)
window = tk.Tk()
window.title("URL Shortener")
window.geometry("700x600")
app_title = tk.Label(window, text="URL Shortener")
app_title.pack()
url_input_space = tk.Frame(window)
url_input_space.pack()
url_input_label = tk.Label(url_input_space, text="Please enter your URL: ")
url_input_label.pack(side=tk.LEFT)
url_input_space_entry = tk.Entry(url_input_space, width=45)
url_input_space_entry.pack()
result_frame = tk.Frame(window)
result_frame.pack()
result_label = tk.Label(result_frame, text="Result: ")
result_label.pack(side=tk.LEFT)
show_result = tk.Entry(result_frame, width=34)
show_result.pack()
shorten_url_button = tk.Button(window, text="Shorten it", command=shorten_url)
shorten_url_button.pack()
window.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.