程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python GUI case: figure guessing idiom development (Part 2)

編輯:Python

Python GUI Case of guessing idioms by looking at pictures ( Second articles )

  • Preface
  • Look at the picture and guess idioms. Applet development ( Second articles )
    • Game selection mode page
    • Game training mode page

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 )


Preface

We are going to implement these functions :

  • One , Game home page : In the home page, you need to draw a title to guess idioms by looking at pictures , Define two button functions ( Start the game , Quit the game ), There is also a function to input the game nickname and verify whether the nickname is empty , To start the game ;
  • Two , Game selection mode page : Click start game on the home page , Enter the selection mode page of the game , It is divided into two modes: training mode and breakthrough mode ;
  • 3、 ... and , Game training mode page : Load the idiom picture , Only guess idioms ( A picture , An input box , A button ) And the accuracy of the answer ;
  • Four , Game entry mode page : How many levels will be customized ,16 Chinese character prompt (12 Randomly generated interfering Chinese characters ), The time taken for game clearance records .

( Material extraction :https://download.csdn.net/download/qq_59142194/85827790)


Look at the picture and guess idioms. Applet development ( Second articles )


Game selection mode page

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 ')

Game training mode page

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 ')

Here we are. , These are the main codes in the game training mode , As for this part of the complete code is not cv down , I will post all the code later .
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved