[英]How to properly organize this code?
我正在创建一个基本的剪刀石头布游戏,以熟悉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()
Tkinter.Tk
继承 mainloop
__init__
构造函数 首先,我们要求父类进行初始化,这是Tkinter.Tk。 初始化 (个体经营)
然后我们将以前的root
称为self
。
Logic
是作为独立的类实现的,它知道前端,只希望调用其方法。
我们预先实例化logic
并将其传递给Gui构造函数。
必须有一个合同,逻辑将要提供什么方法和属性。
当Gui发现时,有一些与逻辑有关的事件,它可以调用logic
方法。
更改逻辑后,通常需要(使用Gui方法)重新渲染某些东西。
这将是遵循的模式:
翻译成Python:
logic = GameLogic()
game = Gui(logic)
game.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.