Necklace1226
Necklace1226

Reputation: 3

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.

The project is a URL shortener.

(I was not allowed to post actual photo out here yet) Click here to see the picture that illustrated how my project looks.

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:

Please enter your URL: xxxx.com (Example 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 e.g. 9eDS34, A1e2wS, etc.)

But now, the RESULT frame didn't show anything after I pressed the button. (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!

Upvotes: 0

Views: 58

Answers (1)

larticho
larticho

Reputation: 169

You can't use configure method with Entry widget. 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()

Upvotes: 2

Related Questions