Tkinter - 从课堂返回并致电新课程

时间:2017-04-06 12:48:53

标签: python python-3.x class tkinter

我试图在python 3.4中创建我自己的游戏Go Fish(现在有两个玩家)。我的游戏有两个主要阶段。第一个是确定谁将首先采取行动。这是通过调用我的类FirstStage(file2.py)完成的,它基本上是手游戏Rock Paper Scissors - 我对抗机器人。获得胜利后,FirstStage返回一个整数(0或1),表示此阶段的获胜者。然后调用SecondStage类(file3.py)将self.winner作为参数传递。游戏脚本分为许多文件,因为我使用了很多类。这是基本的想法,但由于某种原因,FistStage没有返回任何东西。有人可以帮我这个吗?这是我的代码:

file1.py

from file2 import *
from file3 import *
import tkinter as 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 we call FirstStage class to determine who starts first
        self.c = FirstStage(parent)
        self.winner = self.c.getwinner()
        #here we call SecondStage call to start the 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 as tk
from functools import partial
from PIL import ImageTk
from 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 is created
        #buttons and images are place in a grid
        self.rock_paper_scissors()

    def rock_papper_scissors(self):
        #here goes some code 
        #player against API 
        #undefine number of times until there is a winner


    def close(self, w):
        self.grid_forget()
        self.destroy()
        # here buttons, images and frame are destroy
        # save winner
        self.winner = w

    def getwinner(self):
        #return int - 0 for player and 1 for robot(opponent)
        return self.winner

file3.py

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

1 个答案:

答案 0 :(得分:0)

这很长,所以我无法将其作为评论提供。

我按原样测试了您的代码(稍作更改),FirstStage返回了正确的获胜者,目前为0或您在self.winner = 0中提供的任何值。

  1. file2.py 中,canv方法中有__init__但已通过 parenttk.Frame.__init__。它给了我错误,因为这些 参数不匹配,因此我将canv更改为parent
  2. 方法定义期间rock_paper_scissors的拼写和 __init__方法中的方法调用不同,请检查 拼写。
  3. 您尝试更改 file2.py self.winner方法中的close,但该方法未在您提供的代码中的任何位置调用,因此{{1}保持不变(值为self.winner)。
  4. 如果您在 file1.py 0之后添加print语句,您会发现获胜者是 确实回来了。
  5. 如果您进行了这些更改但仍然无法获得正确的获胜者,那么问题可能出在self.winner = self.c.getwinner()中您实施FirstStage的方式;这个问题没有提供。

    以下是我用于测试的代码:

    file1.py

    file2.py

    file2.py

    from file2 import *
    from file3 import *
    import tkinter as 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 we call FirstStage class to determine who starts first
            self.c = FirstStage(parent)
            self.winner = self.c.getwinner()
            print(self.winner)
            #here we call SecondStage call to start the real game
            self.d = SecondStage(self.winner)
    
    if __name__ == "__main__":
        # create main window
        root = tk.Tk()
        root.geometry("800x800")
        start_gui(root)
        root.mainloop()
    

    file3.py

    import tkinter as tk
    from functools import partial
    from PIL import ImageTk
    from 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 is created
            #buttons and images are place in a grid
            self.rock_paper_scissors()
    
        def rock_paper_scissors(self):
            pass
            #here goes some code 
            #player against API 
            #undefine number of times until there is a winner
    
    
        def close(self, w):
            self.grid_forget()
            self.destroy()
            # here buttons, images and frame are destroy
            # save winner
            self.winner = w
    
        def getwinner(self):
            #return int - 0 for player and 1 for robot(opponent)
            return self.winner