Python GUI The case of figure guessing idiom development ( Chapter one )
Python GUI The case of figure guessing idiom development ( Third articles )
Python GUI The case of figure guessing idiom development ( Conclusion )
We are going to implement these functions :
( Material extraction :https://download.csdn.net/download/qq_59142194/85827790)
This page is very simple , Only create two tag words on the page , It is divided into training mode and breakthrough mode , And realize the function of label binding mouse click event .
Effect of implementation
Code implementation
l1.bind(“”,self.game_train_mode)、l2.bind(“”, self.game_chuangguan_mode) It mainly realizes the binding mouse click function ( Left mouse button ).
# Game mode selection page
class game_modeSelection_page(ttkbootstrapWindow):
def __init__(self,nickname):
super().__init__()
self.nickname = nickname
self.page()
def page(self):
self.window_middle(500,300)
self.frame = ttk.Frame(self.root)
self.frame.pack(fill=BOTH, expand=YES)
self.bg = ttk.PhotoImage(file='../sucai/bg2.png')
ttk.Label(self.frame,anchor='nw', image=self.bg).pack()
l1 = ttk.Label(self.frame,text=' Training mode ', font=(' Chinese Xingkai ', 32),relief=RAISED,cursor='hand2',bootstyle=WARNING,background='#324762')
l1.place(x=150,y=60)
l1.bind("<Button-1>",self.game_train_mode)
l2 = ttk.Label(self.frame, text=' Break through mode ', font=(' Chinese Xingkai ', 32),relief=RAISED,cursor='hand2',bootstyle=SUCCESS,background='#324762')
l2.place(x=150, y=140)
l2.bind("<Button-1>", self.game_chuangguan_mode)
def game_train_mode(self,event):
print(' Game training mode ')
def game_chuangguan_mode(self,event):
print(' Game entry mode ')
Load the idiom picture , Only guess idioms ( A picture , An input box , A button ) And the accuracy of the answer .
In fact, before realizing the above functions , I extracted this part separately , In this way, it is convenient to directly call ( Because in these two modes , Their backgrounds are loaded 、 The above words prompt and return button are common ).
Code implementation
The code in this is also very simple .
class game_same_components(ttkbootstrapWindow):
def __init__(self):
super().__init__()
def same_page(self,nickname):
self.nickname = nickname
self.window_middle(960, 540)
self.canvas = ttk.Canvas(self.root)
self.canvas.pack(fill=BOTH, expand=YES)
self.bg = ttk.PhotoImage(file='../sucai/bg3.png')
self.canvas.create_image(0, 35, anchor='nw', image=self.bg)
self.canvas.create_rectangle(0, 0, 960, 35, fill='#F4F4F4', outline='#F4F4F4')
nickname_lable = ttk.Label(self.canvas, text=f' welcome :【{
self.nickname}】 Players go online ', font=20, bootstyle=INFO,background='#F4F4F4')
nickname_lable.place(x=960, y=4)
def nickname_lable_move(rate):
rate += 5
nickname_lable.place(x=960 - rate, y=4)
if rate < 960:
nickname_lable.after(50, nickname_lable_move, rate % 960)
nickname_lable_move(0)
self.return_button_img = ttk.PhotoImage(file='../sucai/return.png')
self.return_button = ttk.Button(self.canvas, bootstyle=(LIGHT, "outline-toolbutton"), image=self.return_button_img,command=self.return_game_modeSelection_page)
self.return_button.place(x=0, y=35)
def return_game_modeSelection_page(self):
self.canvas.destroy()
game_modeSelection_page(self.nickname)
Okay , After extracting the common functions, we can start to complete the function of training mode .
It mainly realizes the accurate calculation of the answer
Let's first define two variables
answer_times = 0 # Record the total number of responses
answer_correct_times = 0 # Record the number of correct answers
First create a label (self.accuracy_lable2) Used to display accuracy , Enter the idiom in the input box, and then enter the keyboard or click the mouse button to bind the following answer() Method . The implementation record only needs to be executed once answer() Methods answer_times += 1(# Record the total number of responses ), If the answer idiom we enter in the input box is consistent with the real answer , will answer_correct_times += 1( # Record the number of correct answers ), conversely , Wrong answer (answer_correct_times) No more 1.
So we can get through answer_correct_times / answer_times To calculate the accuracy
(round((self.answer_correct_times / self.answer_times) * 100, 2) , Percentage with two decimal places )
And the correct answer will automatically go to the next level , perform (self.loading_idiom_img() Method ).
self.answer_idiom_entry.bind("<Return>", lambda event: self.answer())
answer_times = 0 # Record the total number of responses
answer_correct_times = 0 # Record the number of correct answers
# Judge whether the answer is correct
def answer(self):
if self.answer_idiom_entry.get().strip():
self.answer_times += 1
if self.answer_idiom_entry.get().strip() == self.idiom_result:
Messagebox.show_info(message=" Congratulations , Correct answer !!!")
self.loading_idiom_img()
self.answer_idiom_entry.delete(0,'end')
self.answer_correct_times += 1
else:
if not Messagebox.yesno(message=" Wrong answer !!!\n Continue to answer ?") == 'Yes':
self.loading_idiom_img()
self.answer_idiom_entry.delete(0, 'end')
self.accuracy_lable2.config(text=f'{
round((self.answer_correct_times / self.answer_times) * 100, 2)}%')
So I have to write a self.loading_idiom_img() Method , Each time the image loaded in the method is generated by random.choice(os.listdir(‘…/ Look at the picture and guess the idiom ’)) Look at the picture and guess a randomly selected picture under the idiom folder .
# Load idiom pictures
def loading_idiom_img(self):
self.loading_img_times += 1
self.idiom = random.choice(os.listdir('../ Look at the picture and guess the idiom '))
self.result = self.idiom.split('.')[0]
print(' answer :',self.result)
self.idiom_img = ttk.PhotoImage(file=f'../ Look at the picture and guess the idiom /{
self.idiom}')
lm = ttk.Label(self.canvas,image=self.idiom_img)
lm.place(x=215,y=115)
guanqia_lable = ttk.Label(self.canvas, font=(' Chinese Xingkai ', 32),background='#48A6B0')
guanqia_lable.place(x=300,y=450)
guanqia_lable.config(text=f' The first {
self.loading_img_times} Turn off ')