我运行了以下程序:
'''
Application:Tic tac toe
Created by Green Apple
'''
from sys import argv
from random import choice
from functools import partial as potato
from pickle import load,dump
from PyQt6 import QtGui,QtCore,QtWidgets
from UI import Ui_GameUi
class GameUi(QtWidgets.QMainWindow,Ui_GameUi):
"Main class of the app(GameUi)"
reset_checklist = [None for i in range(9)]
def __init__(self,parent=None):
super(GameUi,self).__init__(parent)
self.setupUi(self)
#load
try:
with open("data.pkl",'rb') as f:
data = load(f)
except:
data = [["Python",0],["C++",0],0]
#self.PN[0] is PN's name
#self.PN[1] is PN's win times
self.P1 = data[0]
self.P2 = data[1]
self.All_times = data[2]
#settings
self.NameP1.setText(self.P1[0])
self.NameP2.setText(self.P2[0])
#connections
self.Start_KillB.clicked.connect(self.Start_Kill)
self.__Blist = list([eval(f"self.B{i}") for i in range(1,9+1)])
self.__Slist = list([eval(f"potato(self.BC,index={i})") for i in range(9)])
for i in range(9):
self.__Blist[i].clicked.connect(self.__Slist[i])
self.action_Start_Kill.triggered.connect(self.Start_Kill)
self.actionRename_P_1.triggered.connect(self.RP1)
self.actionRename_P_2.triggered.connect(self.RP2)
self.actionC_lear_data.triggered.connect(self.Reset_data)
self.P1RenameB.triggered.connect(self.RP1)
self.P1RenameB.triggered.connect(self.RP2)
#reset
self.reset()
def reset(self):
"Reset this game"
for button in self.__Blist:
button.setIcon(QIcon(""))
self.checklist = list(self.__class__.reset_checklist)
self.is_game = False
self.Start_KillB.setText("Start")
if All_times == 0:
value1,value2 = 0,0
else:
value1 = int(self.P1[1] / self.All_times * 100)
value2 = int(self.P2[1] / self.All_times * 100)
self.P1Winrate.setProperty("value",value1)
self.P2Winrate.setProperty("value",value2)
self.turn = None
def Start_Kill(self):
"Start or kill this game"
if self.is_game:
self.warn()
else:
self.turn = choice([1,2])
QtWidgets.QMassageBox.information(self,"Foodie",f"P{self.turn} turn first")
self.is_game = True
self.Start_KillB.setText("Kill")
def warn(self):
"warn"
ch = QtWidgets.QMassageBox.warning(self,"Foodie","The game is still running,\ndo you want to kill it? (No result)\n",QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,QtWidgets.QMessageBox.No)
if ch == QtWidgets.QMessageBox.Yes:
self.reset() #No result
def BC(self,index):
"Button clicked"
if not self.is_game:
return
if self.turn == 1 and not self.checklist[index]:
self.checklist[index] = 'X'
self.__Blist[index].setIcon(QIcon("X.png"))
elif self.turn == 2 and not self.checklist[index]:
self.checklist[index] = 'O'
self.__Blist[index].setIcon(QIcon("O.png"))
self.check_win()
if self.turn:
self.turn = (self.turn == 2 and 1 or 2)
def check_win(self):
"check win"
def check(a,b,c):
"A single check function in the local scope GameUi.check_win"
if self.checklist[a] == self.checklist[b] and self.checklist[b] == self.checklist[c] and self.checklist[a]:
return True
else:
return False
win = None
if check(1,2,3) or check(4,5,6) or check(7,8,9):
#Horizontal
win = self.turn
elif check(1,4,7) or check(2,5,8) or check(3,6,9):
#Vertical
win = self.turn
elif check(1,5,9) or check(3,5,7):
#Slash
win = self.turn
else:
#Draw
full = True
for c in self.checklist:
if not c:
#if c == None,full is false
full = False
break
if full:
win = 3
if win == 1:
self.All_times += 1
self.P1[1] += 1
QtWidgets.QtWidgets.QMassageBox.information(self,"Foodie",f"{self.P1[0]} is win!")
self.reset()
elif win == 2:
self.All_times += 1
self.P2[1] += 1
QtWidgets.QtWidgets.QMassageBox.information(self,"Foodie",f"{self.P2[0]} is win!")
self.reset()
elif win == 3:
self.All_times += 1
QtWidgets.QtWidgets.QMassageBox.information(self,"Foodie",f"Both players are draw.")
self.reset()
def CloseEvent(self,event):
if self.is_game:
#if game is running,warn
self.warn()
if self.is_game:
#if game still running,ignore
event.ignore()
else:
#if game doesn't running,save data and quit
with open("data.pkl",'wb') as f:
data = [self.P1,self.P2,self.All_times]
dump(data,f)
f.close()
event.accept()
def RP1(self):
"Rename P1"
name,ok = QtWidgets.QInputDialog.getText(self,"Foodie",f"{self.P1[0]},Enter you new name:\n(Maybe it's your bad hands,please click 'Cancel')")
if name and ok and (not name == self.P2[0]):
self.P1[0] = name
elif name == self.P2[0]:
QtWidgets.QMessageBox.critcal(self,"Foodie","You can't imitate others!")
def RP2(self):
"Rename P2"
name,ok = QtWidgets.QInputDialog.getText(self,"Foodie",f"{self.P2[0]},Enter you new name:\n(Maybe it's your bad hands,please click 'Cancel')")
if name and ok and (not name == self.P1[0]):
self.P2[0] = name
elif name == self.P1[0]:
QtWidgets.QMessageBox.critcal(self,"Foodie","You can't imitate others!")
def Reset_data(self):
"Reset data"
if is_game:
QtWidgets.QMessageBox.critcal(self,"Foodie","You can't clean your score when game is running!")
return
ch = QtWidgets.QMassageBox.question(self,"Foodie","Do you want to be clear your score?",QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No,QtWidgets.QMessageBox.No)
if ch == QtWidgets.QMassageBox.Yes:
self.P1[1] = 0
self.P2[1] = 0
self.All_times = 0
#All slots are definded
if __name__ == '__main__':
app = QtWidgets.QApplication(argv)
window = GameUi()
window.show()
app.exec_()
出现了以下错误:
Traceback (most recent call last):
File "D:\Bean\Python\Application\Tic tac toe\Tic tac toe.pyw", line 182, in <module>
window = GameUi()
File "D:\Bean\Python\Application\Tic tac toe\Tic tac toe.pyw", line 34, in __init__
self.__Blist = list([eval(f"self.B{i}") for i in range(1,9+1)])
File "D:\Bean\Python\Application\Tic tac toe\Tic tac toe.pyw", line 34, in <listcomp>
self.__Blist = list([eval(f"self.B{i}") for i in range(1,9+1)])
File "<string>", line 1, in <module>
NameError: name 'self' is not defined
请问这是怎么回事?
用eval(...,{'self':self}) 传进去,但你B1~B9也没定义呀