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

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

編輯:Python

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

  • Preface
  • Look at the picture and guess idioms. Applet development ( Third articles )
    • Game entry mode page

Python GUI The case of figure guessing idiom development ( Chapter one )
Python GUI The case of figure guessing idiom development ( Second 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 ( Third articles )

Finally came to the third chapter !!!


Game entry mode page

Here we will customize how many levels there are ,16 Chinese character prompt (12 Randomly generated interfering Chinese characters ), The time taken for game clearance records , Mainly pay attention to the following 3 Implementation function of .

Custom levels
When defining the number of levels ( Default 10 Turn off ), We need to click the pass mode on the game selection mode page and add ttkbootstrap There is a query box Querybox, Then enter the number of levels according to the prompt, and you will enter the game entry mode page .

 def game_chuangguan_mode(self,event):
# Default 10 A level (initialvalue=10)
number = Querybox.get_integer(prompt=" Please set the number of levels :",title=" Customize the number of levels ",initialvalue=10,minvalue=0,maxvalue=50)
if number:
self.frame.destroy()
game_chuangguan_page(self.nickname,number)

① Create four labels to display the selection results , Here we need to define a self.answer_list List to load the generated label object , In order to configure the text on the label later configure().

# Create four labels for selecting results 
def create_selection_result_label(self):
self.answer_list = []
for i in range(4):
label = ttk.Label(self.canvas, text='', font=(" Microsoft YaHei ", 35), background='', width=2, cursor='hand2')
label.place(x=130 + i * 100, y=450)
self.answer_list.append(label)

Reselection button , After clicking execute, it will self.answer_list The label objects in the list are all destroyed and recreated .

 # Reselection 
def update_label(self):
self.CLICKTIMES = 0
self.TRUEANSWER = ''
for i in self.answer_list:i.destroy()
self.create_selection_result_label()

② Create... For selecting text content 16 A label (4 x 4), There will be 12 Disturbing words , And when creating labels, a mouse click event is bound to each label object click_label(event).

# Create a label for selecting text content 
def create_option_text_label(self):
def click_label(event):
if self.CLICKTIMES < 4:
self.CLICKTIMES += 1
label_text = event.widget["text"] # Get the text on the label 
self.answer(label_text)
self.label_oop_list = []
# Set up 4 That's ok 4 The label of the column 
for i in range(4):
for j in range(4):
label = ttk.Label(self.canvas, text='', font=(" Microsoft YaHei ", 35), background='#FFFAE3', width=2,cursor='hand2')
label.place(x=510 + j * 100, y=90 + i * 95)
label.bind("<Button-1>", click_label)
self.label_oop_list.append(label)

Here is also an important part , Variable IDX Record the level ,loading_idiom_img() Method for The loop is created for the previous 16 Configure text for text labels ( Include
Correct 4 A word ([i for i in self.idiom]) and
12 Disturbing words (disturb_text_list ))

 IDX = 1 # Which level , Default No 1 Turn off 
# Load idiom pictures 
def loading_idiom_img(self,):
self.idiom = self.idiom_list[self.IDX - 1].split('.')[0]
print(' answer :', self.idiom)
disturb_text_list = [self.GBK2312() for i in range(12)] # Random generation 12 Disturbing Chinese characters 
disturb_text_list.extend([i for i in self.idiom])
for label_oop in self.label_oop_list:
text = random.choice(disturb_text_list)
disturb_text_list.remove(text)
label_oop.configure(text=text)
self.guanqia_lable.config(text=f' The first {
self.IDX} / {
len(self.idiom_list)} Turn off ')
self.idiom_img = ttk.PhotoImage(file=f'../ Look at the picture and guess the idiom /{
self.idiom}.png')
self.lm.configure(image=self.idiom_img)

Randomly generate disturbing words

 # Randomly generate a Chinese character 
def GBK2312(self, ):
head = random.randint(0xb0, 0xf7)
body = random.randint(0xa1, 0xfe)
val = f'{
head:x}{
body:x}'
str = bytes.fromhex(val).decode('gb2312')
return str

③ Recording time , The writing method here is the same as the previous dynamic movement principle , It's all by after() Realization , interval 1 Second refresh label (time_label ) once .self.flag = True To define a semaphore , Used when we finish the game and pass ,run() End of cycle

# Record the time taken for customs clearance 
def recording_time(self):
self.flag = True # Define a semaphore , Used when we finish the game and pass ,run() End of cycle 
time_label = ttk.Label(self.canvas,text=' Duration :00:00:00', font=(" Chinese Xingkai ", 15), background='#DAEFE6',bootstyle=DANGER)
time_label.place(x=730,y=50)
start_time = datetime.datetime.now()
def run():
if self.flag:
time_label.after(1000, run)
update_time = datetime.datetime.now() - start_time
self.time_ =f' Duration :{
update_time}'.split('.')[0]
time_label.configure(text=self.time_) # The millisecond value of the duration is not displayed 
run()

Prompt whether the answer is correct , A little knowledge of threads is used here , Answer the prompt of correct thread startup “ Correct answer ” label ,3 It will disappear automatically in seconds ; Wrong answer , Empathy .

 t = threading.Thread(target=self.dispaly_answer_result, args=(' return \n answer \n wrong \n By mistake ',))
t.setDaemon(True)
t.start()
# Show whether the answer is correct 
def dispaly_answer_result(self,text):
self.result_label.configure(text=text)
time.sleep(3)
try: self.result_label.configure(text='')
except Exception as e: print(e)

That's all for the third article !
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved