<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Python標準圖形化界面GUI庫入門指南– Tkinter

          共 6053字,需瀏覽 13分鐘

           ·

          2021-04-06 14:23

          介紹
          圖形用戶界面(GUI)是為用戶交互提供靈活性的界面。它的交互性使我著迷。應用程序越吸引人,游戲開發(fā)就越好。
          GUI的最常見組件是主窗口,按鈕,文本輸入,標簽,菜單等。在Python中進行GUI開發(fā)的最常見選項是Tkinter,wxPython和JPython。
          我們將在本文中討論Tkinter,創(chuàng)建一個小游戲并可視化結果。

          Tkinter概述

          Python提供了一個名為Tkinter的標準GUI庫。Tkinter模塊有助于快速簡便地創(chuàng)建GUI應用程序。Tkinter提供了15種類型的小部件。一些常見的按鈕,標簽,框架,菜單。消息,單選按鈕,文本,滾動條等。
          在本文中,我們將創(chuàng)建一個小游戲。用戶將持續(xù)獲得數字問題。他們將回答并單擊Enter進入下一個問題,直到他們決定退出并處理結果。
          正確和不正確的答案將被捕獲,以在最后顯示結果。我們將使用標簽,結束,條目,文本,按鈕之類的小部件。事不宜遲,讓我們開始實施吧!

          執(zhí)行

          1.導包

          將所有包分開導入是個好習慣。
          import tkinter
          import random
          from random import randint
          from tkinter import Button
          import matplotlib.pyplot as plt
          import numpy as np

          2.創(chuàng)建一個GUI窗口和全局變量聲明

          我們將創(chuàng)建界面布局。確定版面大小和一個引人注目的標題
          root = tkinter.Tk()
          root.title("Are you smart!!")
          root.geometry("400x200")
          correct_result=0
          correct_answers=0
          total_questions=0
          incorrect_answer=0

          3.評估結果的函數

          我們將創(chuàng)建一個小小的函數定義以完成使代碼易于維護和易于閱讀的任務
          def evaluate(event):
              global correct_result
              global user_input
              user_input_given = user_input.get()
              if str(user_input_given) == str(correct_result):
                  global correct_answers
                  correct_answers += 1
                  nextQuestion()
              else:
                  global incorrect_answer
                  incorrect_answer += 1
                  result = tkinter.Label(root, text="Hard Luck!!nThe correct answer is : "+str(correct_result), font=('Helvetica', 10))
                  result.pack()
                  nextQuestion()
                  root.after(1500, result.destroy)

          4.創(chuàng)建問題的函數

          我們將使用random來創(chuàng)建隨機整數,并為'+','-'和'*'運算符創(chuàng)建隨機選擇,以便用戶可以獲取隨機的數字問題集來回答
          def nextQuestion():
              user_input.focus_set()
              user_input.delete(0, tkinter.END)
              global first_num
              first_num = randint(1,15)
              global second_num
              second_num = randint(1,15)
              global character
              character = random.choice("+-*")
              global correct_result
              if character == '*':
                  correct_result = first_num*second_num
              if character == '+':
                  correct_result = first_num+second_num
              if character == '-':
                  correct_result = first_num-second_num
              text="Enter the value of "+str(first_num)+" "+character+" "+str(second_num)
              global total_questions
              total_questions += 1
              user_question.config(text=text)
              user_question.pack()

          5.退出函數

          我們將創(chuàng)建一個微型函數以退出并記錄結果
          def exitThis():
              print("Total Questions attended : "+str(total_questions))
              print("Total Correct Answers : "+str(correct_answers))
              print("Total Incorrect Answers : "+str(incorrect_answer))
              root.destroy()

          6.最初的問題

          我們將基于隨機整數和數字運算符的隨機集合創(chuàng)建問題的初始集合
          first_num = randint(1,15)
          second_num = randint(1,15)
          character = random.choice("+-*")
          if character == '*':
              correct_result = first_num*second_num
          if character == '+':
              correct_result = first_num+second_num
          if character == '-':
              correct_result = first_num-second_num

          7.標簽創(chuàng)建

          我們將創(chuàng)建界面的文本和布局
          user_question = tkinter.Label(root, text="Enter the value of "+str(first_num)+" "+character+" "+str(second_num), font=('Helvetica', 10))
          user_question.pack()
          user_input = tkinter.Entry(root)
          root.bind('<Return>',evaluate)
          user_input.pack()
          user_input.focus_set()
          exitButton = Button(root, text="EXIT and Check Result"command=exitThis)
          exitButton.pack(side="top", expand=True, padx=4, pady=4)

          8.啟動GUI

          root.mainloop()

          9.結果可視化

          在用戶決定退出游戲后,我們將使用條形圖和餅形圖向用戶顯示結果
          #Plotting the bar graph
          plt.figure(0)
          objects = ('Total Number of Questions','Correct Answers','Incorrect answers')
          y_pos = np.arange(len(objects))
          stats = [total_questions,correct_answers,incorrect_answer]
          plt.bar(y_pos, stats, align='center', alpha=0.5)
          plt.xticks(y_pos, objects)
          plt.ylabel('Numbers')
          plt.title('Your Result!')
          #Plotting the pie chart
          if str(total_questions) != "0":
          plt.figure(1)
          labels = 'Correct Answers','Incorrect answers'
          sizes = [correct_answers,incorrect_answer]
          colors = ['green''red']
          explode = (0.1, 0) # explode 1st slice
          plt.pie(sizes, explode=explode, labels=labels, colors=colors,
          autopct='%1.1f%%', shadow=True, startangle=140)
          plt.axis('equal')

          #Show both the graphs
          plt.show()

          結論

          如果你走到這一步,那么你一定真的很感興趣!
          讓我們看看它最終的樣子
          你可以點擊以下鏈接查看代碼
          https://github.com/prachiprakash26/Play-with-Numbers



          10000+人已加入「大毛CV」

                 

                 



          敬正在努力的我們! 

          瀏覽 50
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  狼友视频在线观看 | 天天干在线影院 | 伊人网视频在线观看 | 高清无碍一区二区三区 | 九九视频免费观看 |