2016-04-30 190 views
0

我正在python中构建一个非常基础的电影推荐GUI,我试图让它在选择流派时打开一个新窗口。我可以打开窗口,但我无法将单选按钮分配给新班级。我希望能够选择一种流派,然后点击下一步,并根据用户选择的按钮开始我的推荐。在tkinter 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() 
+0

*我希望能够选择一个流派,点击下一步,并基于此按钮,用户选择与我的建议开始* - 如果你的意思是你想让第二类知道选择了什么,只需将它发送到movie2,那么self.app = movie2(self.newWindow,genre_selected) –

回答

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) 
     ...