在tkinter GUI中使用多个窗口

时间:2016-04-30 03:14:40

标签: python user-interface tkinter

我正在python中构建一个非常基础的电影推荐GUI,我试图在选择一个类型时打开一个新窗口。我可以打开窗口,但是我无法将单选按钮分配给新课程。我希望能够选择一种类型,然后点击下一步,并根据用户选择的按钮开始推荐。

from tkinter import *
class movie1:
    def __init__(self, master):
        self.master = master
        master.title("Movie Recommendation")

        self.label = Label(master, text= "Welcome to the movie recommendation application! \n Please select the genre of the movie you would like to see.")
        self.label.pack(padx=25, pady=25)

        CheckVar1 = StringVar()

        C1 = Radiobutton(master, text = "Action", variable = CheckVar1, value=1)
        C1.pack(side=TOP, padx=10, pady=10)
        C2 = Radiobutton(master, text = "Comedy", variable = CheckVar1, value=2)
        C2.pack(side=TOP, padx=10, pady=10)
        C3 = Radiobutton(master, text = "Documentary", variable = CheckVar1, value=3)
        C3.pack(side=TOP, padx=10, pady=10)
        C4 = Radiobutton(master, text = "Horror", variable = CheckVar1, value=4)
        C4.pack(side=TOP, padx=10, pady=10)
        C5 = Radiobutton(master, text = "Romance", variable = CheckVar1, value=5)
        C5.pack(side=TOP, padx=10, pady=10)

        self.nextbutton = Button(master, text="Next", command=self.reco)
        self.nextbutton.pack(side=BOTTOM, padx=10, pady=10)

    def reco(self):
        self.newWindow = Toplevel(self.master)
        self.app = movie2(self.newWindow)

class movie2:
    def __init__(self, master):
        self.master = master
        self.frame = Frame(self.master)

    def C1(self):
        print("option 1")    


root = Tk()
my_gui = movie1(root)
root.mainloop()

1 个答案:

答案 0 :(得分:0)

您可以将所选值传递给新类:

class movie1:
    def __init__(self, master):
        ...
        self.CheckVar1 = StringVar()
        ...

    def reco(self):
        ...
        choice = self.CheckVar1.get()
        self.app = movie2(self.newWindow, choice)

class movie2:
    def __init__(self, master, choice):
        ...
        print("you chose: %s" % choice)
        ...