gpt4 book ai didi

python - 如何正确组织这段代码?

转载 作者:行者123 更新时间:2023-12-01 05:14:03 27 4
gpt4 key购买 nike

我正在创建一个基本的石头剪刀布游戏来熟悉 python 和 tkinter。我想创建一个 gui 和一个逻辑类来将两者分开。但是,我似乎找不到在 python 中既有效又对我有意义的代码布局。

我希望 Gui 类只了解小部件并更新它们。

class Gui:      
def setup(self):
root = Tk.Tk()
root.geometry("370x170")
root.resizable(width=False, height=False)
root.title("Rock, Paper, Scissors")
root.iconbitmap("Play.ico")

rock_button = Tk.Button(root, text="Rock", command=rock_clicked)
rock_button.place(width=100, height=30, x=10, y=30)

paper_button = Tk.Button(root, text="Paper", command=paper_clicked)
paper_button.place(width=100, height=30, x=10, y=70)

scissors_button = Tk.Button(root, text="Scissors", command=scissors_clicked)
scissors_button.place(width=100, height=30, x=10, y=110)

score_font = font.Font(family="Helvetica", size=20)

own_score_lbl = Tk.Label(root, text="0", relief=Tk.RIDGE, font=score_font)
own_score_lbl.place(width=50, height=110, x=120, y=30)

ai_score_lbl = Tk.Label(root, text="0", relief=Tk.RIDGE, font=score_font)
ai_score_lbl.place(width=50, height=110, x=200, y=30)

ai_choice = Tk.Label(root, relief=Tk.RIDGE)
ai_choice.place(width=100, height=110, x=260, y=30)

root.mainloop()

gui = Gui()
gui.setup()

在其他语言中,我习惯在 gui 类中拥有逻辑成员变量,反之亦然。这在这里行不通。由于 self 参数的存在,点击处理函数不能是逻辑类的成员。所以我尝试将它们声明为模块级,并从中调用逻辑类的方法,但这也没有成功。

理想情况下,在单击事件之后,我希望调用一个逻辑类方法,然后进行计算,并调用适当的 gui 方法,即 set_label_text()

如何通过 OO 设计来实现这一目标?

最佳答案

我绝对不是 Tkinter 专家,这是我的第一个 Tkinter 应用程序。

但这是我的建议,如何使用 Python 类继承来组织您的解决方案。

代码运行

import Tkinter
import tkFont as font

class Gui(Tkinter.Tk):
def __init__(self, logic):
Tkinter.Tk.__init__(self)

self.logic = logic

self.geometry("370x170")
self.resizable(width=False, height=False)

rock_button = Tkinter.Button(self, text="Rock", command=self.rock_clicked)
rock_button.place(width=100, height=30, x=10, y=30)

paper_button = Tkinter.Button(self, text="Paper", command=self.paper_clicked)
paper_button.place(width=100, height=30, x=10, y=70)

scissors_button = Tkinter.Button(self, text="Scissors", command=self.scissors_clicked)
scissors_button.place(width=100, height=30, x=10, y=110)

score_font = font.Font(family="Helvetica", size=20)

own_score_lbl = Tkinter.Label(self, text="0", relief=Tkinter.RIDGE, font=score_font)
own_score_lbl.place(width=50, height=110, x=120, y=30)

ai_score_lbl = Tkinter.Label(self, text="0", relief=Tkinter.RIDGE, font=score_font)
ai_score_lbl.place(width=50, height=110, x=200, y=30)

ai_choice = Tkinter.Label(self, relief=Tkinter.RIDGE)
ai_choice.place(width=100, height=110, x=260, y=30)

self.render_title()

def render_title(self):
logic = self.logic
templ = "Rock({logic.rock_counter}), Paper({logic.paper_counter}), Scissors({logic.scissors_counter})"
title = templ.format(logic=logic)
self.title(title)

def rock_clicked(self):
self.logic.play_rock()
self.render_title()

def paper_clicked(self):
self.logic.play_paper()
self.render_title()

def scissors_clicked(self):
self.logic.play_scissors()
self.render_title()

class GameLogic():
def __init__(self):
self.rock_counter = 0
self.paper_counter = 0
self.scissors_counter = 0

def play_rock(self):
self.rock_counter += 1

def play_paper(self):
self.paper_counter += 1

def play_scissors(self):
self.scissors_counter += 1

logic = GameLogic()
game = Gui(logic)
game.mainloop()

Gui 继承自 Tkinter.Tk

  • 我们从 Tk 获取所有方法,包括。 主循环

使用 Gui __init__ 构造函数

首先,我们要求父类进行初始化,这是 Tkinter.Tk.init(self)

然后我们将以前的root称为self

向 Gui 添加逻辑

Logic 是作为独立类实现的,它知道前端的注释,它只期望调用它的方法。

我们提前实例化逻辑并将其传递给Gui构造函数。

必须有一个契约,Logic要提供什么方法​​和属性。

使用 Gui 中的逻辑

当Gui发现有一些逻辑相关的事件时,它可以调用logic的方法。

逻辑发生变化后,经常需要(使用 Gui 方法)重新渲染某些内容。

开始吧

这应该是遵循的模式:

  1. 实例化逻辑
  2. 创建 gui,传入逻辑
  3. 让它运行

翻译成Python:

logic = GameLogic()
game = Gui(logic)
game.mainloop()

关于python - 如何正确组织这段代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23595144/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com