猜數字之tkinter

2021-10-12 17:52:12 字數 4159 閱讀 3215

import random

import tkinter

import tkinter.messagebox

defb_clicked()

:if name_entry.get()==

'':tkinter.messagebox.showerror(title=

'error'

, message=

'請輸入使用者名字'

)else

: cin_name = name_entry.get(

)try

: text_check =

''.join(j for j in age_entry.get(

)if j in

'0123456789'

) cin_age =

int(text_check)

text_check =

''.join(j for j in weight_entry.get(

)if j in

'0123456789.'

) cin_weight =

float

(text_check)

text_check =

''.join(j for j in high_entry.get(

)if j in

'0123456789.'

) cin_high =

float

(text_check)

except valueerror:

tkinter.messagebox.showerror(title=

'error'

, message=

'請輸入數字'

) pi = bmi(cin_name, cin_age, cin_weight, cin_high)

pi.search_get(

)class

bmi:

def__init__

(self, name, age, weight, high)

: self.name = name

self.age = age

self.weight = weight

self.high = high

self.bmi = self.weight / self.high / self.high

if self.bmi <

18.5

: self.status =

"健康狀況:偏瘦。"

elif

18.5

<= self.bmi <24:

self.status =

"健康狀況:正常。"

elif

24<= self.bmi <30:

self.status =

"健康狀況:偏胖。"

elif self.bmi >30:

self.status =

"健康狀況:肥胖。"

defsearch_get

(self)

: label2[

'text']=

"當前使用者為{},年齡為{},體重為{},身高為{},\nbmi值為,{}"

.format

(self.name, self.age,

self.weight, self.high, self.bmi,

self.status)

if __name__ ==

"__main__"

: n =

int(random.random()*

50+1)

i =0 window = tkinter.tk(

) window.title(

'bmi計算器'

) window.geometry(

'500x300'

) label1 = tkinter.label(window, text=

'bmi計算器'

, font=

('arial',12

), width=

30, height=2)

label1.place(x=

110, y=

0, anchor=

'nw'

) button1 = tkinter.button(window, text=

'開始計算'

, font=

('arial',12

), width=

20, height=

1, command=b_clicked)

button1.place(x=

150, y=

50, anchor=

'nw'

) label2 = tkinter.label(window, text=

'請輸入相關資訊'

, bg=

'green'

, font=

('arial',12

), width=

50, height=3)

label2.pack(side=

'bottom'

) name_entry = tkinter.entry(window, width=

7, show=

none

) name_entry.place(x=

220, y=

90, anchor=

'nw'

) age_entry = tkinter.entry(window, width=

7, show=

none

) age_entry.place(x=

220, y=

120, anchor=

'nw'

) weight_entry = tkinter.entry(window, width=

7, show=

none

) weight_entry.place(x=

220, y=

150, anchor=

'nw'

) high_entry = tkinter.entry(window, width=

7, show=

none

) high_entry.place(x=

220, y=

180, anchor=

'nw'

) label3 = tkinter.label(window, text=

'姓名:'

, font=

('arial',10

), width=

4, height=1)

label3.place(x=

180, y=

90, anchor=

'nw'

) label4 = tkinter.label(window, text=

'年齡:'

, font=

('arial',10

), width=

4, height=1)

label4.place(x=

180, y=

120, anchor=

'nw'

) label4 = tkinter.label(window, text=

'體重:'

, font=

('arial',10

), width=

4, height=1)

label4.place(x=

180, y=

150, anchor=

'nw'

) label4 = tkinter.label(window, text=

'身高:'

, font=

('arial',10

), width=

4, height=1)

label4.place(x=

180, y=

180, anchor=

'nw'

) window.mainloop(

)

猜數字介面版tkinter

import random import tkinter import sys import tkinter.messagebox defcai shu zi global i if button1 text 遊戲結束 sys.exit else try text check join j for ...

學習記錄 tkinter模組猜人名

猜名字 複製貼上即可使用 from tkinter import import random 隨機數字 開啟 def start execute global time list1 周杰倫 劉德華 張學友 林俊傑 阿信 陶喆 a random.sample list1,1 num text a nu...

leetcode解題之猜數字遊戲

你正在和你的朋友玩 猜數字 bulls and cows 遊戲 你寫下乙個數字讓你的朋友猜。每次他猜測後,你給他乙個提示,告訴他有多少位數字和確切位置都猜對了 稱為 bulls 公牛 有多少位數字猜對了但是位置不對 稱為 cows 奶牛 你的朋友將會根據提示繼續猜,直到猜出秘密數字。請寫出乙個根據秘...