Home > Mobile >  Can you use an input to open a Tkinter Window?
Can you use an input to open a Tkinter Window?

Time:10-02

I am currently in year 11 doing computer science, im making a glossary of key terms using tkinter. I was wondering if i can use a user input to open a tkinter window. e.g

topic = input("Choose a topic, 1. Theory or 2. Algorithms: ")

if topic == '1':
       then here i would call the theory tkinter window using a subroutine

i have tried this but it doesnt seem to work for me.

Below is the link to the whole code

https://i.stack.imgur.com/WDfZP.png

This is what the UI looks like so far

https://i.stack.imgur.com/0zI37.png

CodePudding user response:

Rather than making it too complex, just write the following code:

from tkinter import *

topic = input("Choose a topic, 1. Theory or 2. Algorithms: ")
    
def topic_1():
    root = Tk()

    # Your Code here

    root.mainloop()

def topic_2():
    root = Tk()

    # Your Code here

    root.mainloop()

if topic == '2':
    topic_2()    

elif topic == '1':
    topic_1()

CodePudding user response:

Here this is the best I could do for you as I didn't want to re-type all of your code. I would recommend replacing windows.mainloop with a while loop and window.update

from tkinter import *

window = Tk()
window.title("Something")
window.geometry("1000x1000")

text_box = Text()
text_box.pack()
new_window = False


def new_window_fun():
    window2 = Tk()
    window2.title("Something new :)")
    window2.geometry("1000x1000")
    label = Label(text="Hello")
    label.pack()
    while True:
        window2.update()


try:
    while 'normal' == window.state():
        if text_box.get("1.0", END).strip() == "new window" and new_window is False:
            new_window = True
            window.destroy()  # close the current window
            new_window_fun()
            print("finished")
        else:
            window.update()
except:
    print("the end")
  • Related