python - Tkinter - Returning from a class and calling a new one -


i trying create own version of game go fish (two players now) in python 3.4. game has 2 main stages. first 1 determine going make first move. done calling class firststage (file2.py) hand game rock paper scissors - me against robot. after have winner, firststage returns integer (0 or 1) represents winner of stage. secondstage class (file3.py) called passing self.winner argument. game script split in many files because use many classes. basic idea reason fiststage not returning anything. can me this? here code:

file1.py

from file2 import * file3 import * import tkinter tk  class start_gui(tk.frame):      def __init__(self, parent, *args, **kwargs):         tk.frame.__init__(self,parent, *args, **kwargs)         # create canvas         self.canvas = tk.canvas(parent, width=800, height=800, background="green")         self.canvas.pack()          # here call firststage class determine starts first         self.c = firststage(parent)         self.winner = self.c.getwinner()         #here call secondstage call start real game         self.d = secondstage(self.winner)  if __name__ == "__main__":     # create main window     root = tk.tk()     root.geometry("800x800")     start_gui(root)     root.mainloop() 

file2.py

import tkinter tk functools import partial pil import imagetk pil import image  class firststage(tk.frame):      def __init__(self, canv):         tk.frame.__init__(self, parent, background="black", padx=10, pady=10)         self.winner = 0         #here new frame created         #buttons , images place in grid         self.rock_paper_scissors()      def rock_papper_scissors(self):         #here goes code          #player against api          #undefine number of times until there winner       def close(self, w):         self.grid_forget()         self.destroy()         # here buttons, images , frame destroy         # save winner         self.winner = w      def getwinner(self):         #return int - 0 player , 1 robot(opponent)         return self.winner 

file3.py

class secondstage:     def __init__(self, winner)         #here goes scripts 

this long couldn't provide comment.

i tested code (with minor changes) , firststage returned correct winner, @ moment 0 or whatever value give in self.winner = 0.

  1. in file2.py, have canv in __init__ method passed parent tk.frame.__init__. gave me error since these arguments not match, hence changed canv parent.
  2. the spelling of rock_paper_scissors during method definition , method call in __init__ method different, check spellings.
  3. you tried change self.winner in close method of file2.py, method not called anywhere in code provided, self.winner stays same (having value of 0).
  4. if put print statement after self.winner = self.c.getwinner() in file1.py, you'll realize winner indeed returned.

if make these changes , still don't correct winner, problem might how implemeted firststage in file2.py; not provided in question.

here code used testing:

file1.py

from file2 import * file3 import * import tkinter tk  class start_gui(tk.frame):      def __init__(self, parent, *args, **kwargs):         tk.frame.__init__(self,parent, *args, **kwargs)         # create canvas         self.canvas = tk.canvas(parent, width=800, height=800, background="green")         self.canvas.pack()          # here call firststage class determine starts first         self.c = firststage(parent)         self.winner = self.c.getwinner()         print(self.winner)         #here call secondstage call start real game         self.d = secondstage(self.winner)  if __name__ == "__main__":     # create main window     root = tk.tk()     root.geometry("800x800")     start_gui(root)     root.mainloop() 

file2.py

import tkinter tk functools import partial pil import imagetk pil import image  class firststage(tk.frame):      def __init__(self, parent):         tk.frame.__init__(self, parent, background="black", padx=10, pady=10)         self.winner = 0         #here new frame created         #buttons , images place in grid         self.rock_paper_scissors()      def rock_paper_scissors(self):         pass         #here goes code          #player against api          #undefine number of times until there winner       def close(self, w):         self.grid_forget()         self.destroy()         # here buttons, images , frame destroy         # save winner         self.winner = w      def getwinner(self):         #return int - 0 player , 1 robot(opponent)         return self.winner 

file3.py

class secondstage:     def __init__(self, winner):         pass         #here goes scripts 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -