使用tk框架合并两个应用程序

约根

我有一个用python和tkinter制作的记忆游戏。我还制作了一个使用tk.Frame来显示不同框架的简单GUI的应用程序。我的问题是将记忆游戏放在GUI应用程序的框架之一中。

我有一个用于内存游戏的.py文件和一个用于GUI的文件。GUI具有多个类和框架。我想将记忆游戏放在这些框架之一中,以便您可以通过菜单导航到游戏中。游戏应仅在导航至时显示。

我试过了:

  • 在GUI文件的顶部导入memorygame文件
  • 在GUI文件的类中导入memorygame文件
  • 将整个内存游戏代码复制到GUI文件的类中

导入文件使两个应用程序都尝试在启动时在不同的窗口中运行。将游戏代码复制到GUI类中会产生很多错误。

我有python 3.7和tkinter 8.6

记忆游戏首先创建一个画布,并在其上进行绘制:

win = tk.Tk()
canvas = tk.Canvas(win, width = 600, height = 480)
canvas.pack()
class Tile(object):
    def __init__(self, x, y, text):
        self.y = y
        self.x = x
        self.text = text
    def drawFaceDown(self):
        canvas.create_rectangle(self.x, self.y, self.x + 100, self.y + 100, fill = "green")
...

这就是我使用类创建显示不同内容的框架的方式:

class PageMG(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        # memory game goes here

如果有人想看,我可以显示整个文件

我希望游戏仅在单击将我带到想要游戏的框架的按钮之前显示。

编辑:整个记忆游戏文件

import tkinter as tk
import time
from random import randint
from random import shuffle
win = tk.Tk()
canvas = tk.Canvas(win, width = 500, height = 500)
canvas.pack()
class Tile(object):
    def __init__(self, x, y, text):
        self.y = y
        self.x = x
        self.text = text
    def drawFaceDown(self):
        canvas.create_rectangle(self.x, self.y, self.x + 70, self.y + 70, fill = "blue")
        self.isFaceUp = False
    def drawFaceUp(self):
        canvas.create_rectangle(self.x, self.y, self.x + 70, self.y + 70, fill = "blue")
        canvas.create_text(self.x + 35, self.y + 35, text = self.text, width = 70, fill = "white", font='Helvetica 12 bold')
        self.isFaceUp = True
    def isUnderMouse(self, event):
        if(event.x > self.x and event.x < self.x + 70):
            if(event.y > self.y and event.y < self.y + 70):
                return True

tiles = []
colors = [
    "Eple",
    "Appelsin",
    "Banan",
    "Agurk",
    "Brokkoli",
    "Tomat",
    "Sitron",
    "Melon",
    "Hvitløk",
    "Erter",
    "Jordbær",
    "Blåbær"
]

selected = []
for i in range(10):
    randomInd = randint(0, len(colors) - 1)
    color = colors[randomInd]
    selected.append(color)
    selected.append(color)
    del colors[randomInd]
shuffle(selected)

flippedTiles = []

def mouseClicked(self):
    global numFlipped
    global flippedTiles
    for i in range(len(tiles)):
        if tiles[i].isUnderMouse(self):
            if (len(flippedTiles) < 2 and not(tiles[i].isFaceUp)) :
                tiles[i].drawFaceUp()
                flippedTiles.append(tiles[i])
            if (len(flippedTiles) == 2):
                if not(flippedTiles[0].text == flippedTiles[1].text):
                    time.sleep(1)
                    flippedTiles[0].drawFaceDown()
                    flippedTiles[1].drawFaceDown()

NUM_COLS = 5
NUM_ROWS = 4

for x in range(0,NUM_COLS):
    for y in range(0,NUM_ROWS):
            tiles.append(Tile(x * 78 + 10, y * 78 + 40, selected.pop()))

for i in range(len(tiles)):
    tiles[i].drawFaceDown()

flippedThisTurn = 0
def mouseClicked(event):
    global flippedTiles
    global flippedThisTurn
    for tile in tiles:
        if tile.isUnderMouse(event):
            if (not(tile.isFaceUp)) :
                tile.drawFaceUp()
                flippedTiles.append(tile)
                flippedThisTurn += 1

            if (flippedThisTurn == 2):
                win.after(1000, checkTiles)
                flippedThisTurn = 0

def checkTiles():
    if not(flippedTiles[-1].text == flippedTiles[-2].text): #check last two elements
        flippedTiles[-1].drawFaceDown() #facedown last two elements
        flippedTiles[-2].drawFaceDown()
        del flippedTiles[-2:] #remove last two elements

win.bind("<Button-1>", mouseClicked)


win.mainloop()
马修·G
  1. 在GUI文件的顶部导入memorygame文件
  2. 在GUI文件的类中导入memorygame文件

这两种方式都会使“应用程序都尝试在不同的窗口中启动”,这是因为memorygame文件创建了一个新tk.Tk()窗口,GUI处理程序也是如此。

将整个内存游戏代码复制到GUI文件的类中

这可能会导致很多问题,具体取决于您将文件复制到的位置,因为这些文件具有自己的导入依赖关系,并且导入路径可能会根据您将文件复制到的位置而变化。

我建议做的是以下操作,我将更改内存游戏的代码以使内存游戏成为class(tk.Frame)(Frame类),然后您应该能够导入内存游戏并将该帧粘贴到GUI中PageMk(tk.Frame),我不知道您的代码具有的依赖关系,但这应该可以工作。

使用提供的代码进行更改的示例

class MemGame(tk.Frame):
    def __init__(self, parent):
        super(MemGame, self).__init__(parent)
        self.configure(width=600, height=480)
        canvas = tk.Canvas(self, width=600, height=480, bg="red")
        canvas.pack()

class Tile:
    def __init__(self, parent, x, y, text):
        self.parent = parent
        self.y = y
        self.x = x
        self.text = text
    def drawFaceDown(self):
        self.parent.create_rectangle(self.x, self.y, self.x + 100, self.y + 100, fill="green")

class PageMG(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        x = MemGame(self)
        x.pack()

我的编辑为完整代码:

import tkinter as tk
import time
from random import randint
from random import shuffle


class Controller(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        if True:
            self.frames = {}
            for F in (PageMG,):
                page_name = F.__name__
                frame = F(parent=container, controller=self)
                self.frames[page_name] = frame

                # put all of the pages in the same location;
                # the one on the top of the stacking order
                # will be the one that is visible.
                frame.grid(row=0, column=0, sticky="nsew")

            self.show_frame("PageMG")
        self.geometry("500x400")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class PageMG(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        x = MemGame(self)
        x.pack()


class Tile(object):
    def __init__(self, canvas, x, y, text):
        self.canvas = canvas
        self.y = y
        self.x = x
        self.text = text

    def drawFaceDown(self):
        self.canvas.create_rectangle(self.x, self.y, self.x + 70, self.y + 70, fill = "blue")
        self.isFaceUp = False

    def drawFaceUp(self):
        self.canvas.create_rectangle(self.x, self.y, self.x + 70, self.y + 70, fill = "blue")
        self.canvas.create_text(self.x + 35, self.y + 35, text = self.text, width = 70, fill = "white", font='Helvetica 12 bold')
        self.isFaceUp = True

    def isUnderMouse(self, event):
        if(event.x > self.x and event.x < self.x + 70):
            if(event.y > self.y and event.y < self.y + 70):
                return True


class MemGame(tk.Frame):
    def __init__(self, master):
        super(MemGame, self).__init__(master)
        self.configure(width=500, height=500)
        self.canvas = tk.Canvas(self, width=500, height=500)
        self.canvas.pack()
        self.tiles = []
        self.colors = [
            "Eple",
            "Appelsin",
            "Banan",
            "Agurk",
            "Brokkoli",
            "Tomat",
            "Sitron",
            "Melon",
            "Hvitløk",
            "Erter",
            "Jordbær",
            "Blåbær"
        ]

        selected = []
        for i in range(10):
            randomInd = randint(0, len(self.colors) - 1)
            color = self.colors[randomInd]
            selected.append(color)
            selected.append(color)
            del self.colors[randomInd]
        shuffle(selected)
        self.flippedTiles = []
        NUM_COLS = 5
        NUM_ROWS = 4

        for x in range(0, NUM_COLS):
            for y in range(0, NUM_ROWS):
                self.tiles.append(Tile(self.canvas, x * 78 + 10, y * 78 + 40, selected.pop()))

        for i in range(len(self.tiles)):
            self.tiles[i].drawFaceDown()
        self.flippedThisTurn = 0
        self.bind("<Button-1>", self.mouseClicked)

    # def mouseClicked(self):
    #     for i in range(len(self.tiles)):
    #         if self.tiles[i].isUnderMouse(self):
    #             if (len(self.flippedTiles) < 2 and not(self.tiles[i].isFaceUp)) :
    #                 self.tiles[i].drawFaceUp()
    #                 self.flippedTiles.append(self.tiles[i])
    #             if (len(self.flippedTiles) == 2):
    #                 if not(self.flippedTiles[0].text == self.flippedTiles[1].text):
    #                     time.sleep(1)
    #                     self.flippedTiles[0].drawFaceDown()
    #                     self.flippedTiles[1].drawFaceDown()

    def mouseClicked(self, event):
        for tile in self.tiles:
            if tile.isUnderMouse(event):
                if (not(tile.isFaceUp)) :
                    tile.drawFaceUp()
                    self.flippedTiles.append(tile)
                    self.flippedThisTurn += 1

                if (self.flippedThisTurn == 2):
                    self.after(1000, self.checkTiles)
                    self.flippedThisTurn = 0

    def checkTiles(self):
        if not(self.flippedTiles[-1].text == self.flippedTiles[-2].text): #check last two elements
            self.flippedTiles[-1].drawFaceDown()
            self.flippedTiles[-2].drawFaceDown()
            del self.flippedTiles[-2:]


if __name__ == '__main__':
    c = Controller()
    c.mainloop()

祝好运 :)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何“合并”两个Python扭曲的应用程序?

在同一Spring应用程序中使用两个日志记录框架

在单个Web应用程序中使用两个或多个不同的前端框架是否正确?

在同一个Rails应用程序中使用两个单独的CSS框架

部署使用两个端口的应用程序

使用composer同时开发两个应用程序

在一个应用程序中合并两个Asp.Net Core API

将两个 Xcode 目标合并为一个可分发的应用程序

Play框架将两个Play应用程序合二为一

如何在两个应用程序之间共享实体框架?

如何优化我的spark应用程序以合并大小大于集群内存的两个rdd?

如何在Java Spring Boot应用程序中合并两个对象?

在flutter应用程序中的Firebase身份验证中合并两个uid

合并两个Application Insights资源时出现未知功能的应用程序

同时使用两个 spring 启动应用程序时注销了一个 spring 应用程序

将两个大型应用程序作为框架集成到一个 Xcode 项目中

如何在Android Manifest中声明两个不同的应用程序或将它们合并为一个应用程序类

如何使用应用程序 A 的两个因素和应用程序 B 的两个因素不使用相同的用户登录?

在使用 Inno Setup 安装的两个应用程序之间共享注册的 .NET 程序集

如何使用一个公共 IP 映射两个 azure 应用程序?

如何在“一个”应用程序中使用两个Parse帐户

如何在两个应用程序中使用一个端口?

在同一应用程序上使用两个Facebook应用程序ID进行测试

是否可以使用Windows应用程序打包项目打包两个UWP应用程序?

如何使用两个不同的节点版本运行两个不同的nodejs应用程序

使用 apache2 和 WSGI 在两个不同的域上运行两个 Flask 应用程序

将两个键盘指定给两个不同的应用程序时,是否可以同时使用两个键盘?

如何连接两个应用程序React / Node?

如何链接两个Java应用程序