2021-04-28

I am trying to update a tkinter label with a variable, but the label just comes up blank

I know there are a lot of similar posts to this, but i could not find a solution. I am trying to update the label with a tk.StringVar variable, but it comes up blank every time run it. If I put text in it works fine. I gave the same variable a different name to see if it made a difference, it did not.

This is the label, and the function is below, thank you for the help!

park1=selected_park.get()
Label(newWindow, textvariable=park1, width=20, fg="black", font=("bold", 12), bd=1, relief="raised").grid(row=0, col

def search_hikes():
    try:
        park = selected_park.get()
        difficulty = selected_diff.get()
        print(park, difficulty)

    except Exception as e:
        print(e)
        messagebox.showinfo("Tkinter", "Select State, Park, and Difficulty.")
        raise Exception

    try:
        rows_p, num_rows_p = search_parks_in_dbs(park, difficulty)
    except Exception as e:
        messagebox.showinfo("Tkinter", "Database error")
        exit()

    if num_rows_p == 0:
        messagebox.showinfo("Database Error", "No results")

    else:
        newWindow = tk.Toplevel(form)
        newWindow.title("Search Results")
        newWindow.geometry("1200x800")
        park1=selected_park.get()
        Label(newWindow, textvariable=park1, width=20, fg="black", font=("bold", 12), bd=1, relief="raised").grid(row=0, column=3)
        table = ttk.Treeview(newWindow, columns=(1, 2, 3, 4, 5, 6, 7), height=80, show="headings")

        table.heading(1, text="Hike Name")
        table.heading(2, text="Length(Mi)")
        table.heading(3, text="Elevation Gain(ft)")
        table.heading(4, text="Nearest City")
        table.heading(5, text="Location")
        table.heading(6, text="Difficulty")
        table.heading(7, text="Route Type")

        table.column(1, width=300)
        table.column(2, width=80)
        table.column(3, width=80)
        table.column(4, width=80)
        table.column(5, width=150)
        table.column(6, width=130)
        table.column(7, width=130)

        labelframe = Frame(newWindow)
        labelframe.grid(row=2, column = 0, columnspan=7, pady=10)

        parklabel =Label(newWindow,textvariable = park, width=20, font=("bold", 12), bd=1, relief="raised")

        table.grid(row=2, column=0, columnspan=7, padx=20, pady=20)

        parklabel.grid(row=2, column = 0, columnspan=7)



        for row in rows_p:
            table.insert('', 'end', values=(row[0], row[1], row[2], row[3], row[4], row[5], row[6]))


def search_parks_in_dbs(park, difficulty):
    try:
        con = open_database()
    except Exception:
        messagebox.showinfo("Database connection error")
        exit()

    messagebox.showinfo("Connected to Database", "DB Connection OK")

    try:
        sql = "SELECT h.hike_name, h.length_miles, h.elevation_gain_feet, h.nearest_city, " \
              "h.geo_location,  d.difficulty_name, r.route_name " \
              "FROM `park_tbl` p, `hike_tbl` h, `park-hike_rtbl` ph, `difficulty_tbl` d, `hike-difficulty_rtbl` hd, " \
              "`route_tbl` r, `hike-route_rtbl` hr " \
              "WHERE p.park_name = %s AND d.difficulty_level =%s AND p.park_id = ph.park_id " \
              "AND h.hike_id = ph.hike_id AND h.hike_id = hd.hike_id AND d.difficulty_id = hd.difficulty_id " \
              "AND h.hike_id = hr.hike_id AND r.route_id = hr.route_id"

        vals = (park, difficulty)
        print(sql)
        num_of_rows, rows = query_database(con, sql, vals)
    except DatabaseError:
        messagebox.showinfo("Error querying the database")

    return rows, num_of_rows


from Recent Questions - Stack Overflow https://ift.tt/2R74wkl
https://ift.tt/eA8V8J

No comments:

Post a Comment