[Python] GUI development issue, pop up with results needs the results displayed differently

Discussão em 'Python' iniciado por Stack, Fevereiro 3, 2025.

  1. Stack

    Stack Membro Participativo

    Ok so I looked through the other posts on the forum and couldn't find a similar question. Here is the situation, I'm working on a project where the user will input a number of choice into two entry boxes and then upon hitting the generate button it will display the results from two different lists. When this is done it opens a new window and posts the results in a horizontal fashion. What I am trying to do is get the results laid out in a grid format where I can have a header with descriptors above each item to where it would almost look like a grid. I will post the current code and a capture of the pop up. Any help on this is greatly appreciated. I've gone through the books that I have as well as the internet and can not find a solution. Thank you in advance.

    import tkinter as tk
    import random

    def generate_random_value():
    try:
    num_values = int(entry.get())
    if num_values > 0 and num_values <= len(my_list):
    random_values = random.sample(my_list, num_values)
    new_window = tk.Toplevel(window)
    new_window.title("Wow!!")
    xresult_label=tk.Label(new_window, text='result')
    xresult_label.pack()
    xresult_label.config(text=random_values)
    else:
    result_label.config(text="Invalid input")
    result_label.pack_forget()
    except ValueError:
    result_label.config(text="Invalid input")
    try:
    num_values = int(entry2.get())
    if num_values > 0 and num_values <= len(my_list2):
    random_values = random.sample(my_list2, num_values)
    yresult_label=tk.Label(new_window, text='result')
    yresult_label.pack()
    yresult_label.config(text=random_values)
    else:
    result_label.config(text="Invalid input")
    result_label.pack_forget()
    except ValueError:
    result_label.config(text="Invalid input")

    window=tk.Tk()
    window.title("Random Value Generator")

    my_list= [10,20,30,40,50,60,70,80,90,10]
    my_list2= ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]

    entry = tk.Entry(window)
    entry.pack()
    entry2 = tk.Entry(window)
    entry2.pack()

    button = tk.Button(window, text="Generate", command=generate_random_value)
    button.pack()

    result_label = tk.Label(window, text="")
    result_label.pack()

    window.mainloop()

    Continue reading...

Compartilhe esta Página