要求
1.单击”出题“按钮时会在label控件中显示新出的题目;
2.单击”判分“按钮时会对用户在文本框中输入的结果进行判断;
3.将题目、用户输入的结果和判分结果一起显示到列表中,✓和✘用以上两个字符来表示。
源码
import tkinter as tk
import random as rd
from tkinter import scrolledtext
ls = ['+' , '-' , '*']
cnts = 0
def click_button1():
global ans_1
a = rd.randint(0,9)
b = rd.randint(0,9)
c = rd.choice(ls)
if(c == "*"):
ans_1 = a * b
str1 = "{}*{}=".format(a, b)
elif(c == "-"):
ans_1 = a - b
str1 = "{}-{}=".format(a, b)
else:
ans_1 = a + b
str1 = "{}+{}=".format(a, b)
result_num.set(str1)
def click_button2(txet):
ans_2 = Entry_ans_num.get()
ans_2 = int(ans_2)
if(ans_1 == ans_2):
text1.insert("end","{}={} √\n".format(result_num.get(),ans_2))
else:
text1.insert("end","{}={} ×\n".format(result_num.get(),ans_2))
root = tk.Tk()# 创建一个窗体
root.title("自动出题判分")# 创建一个标题
root.geometry('300x300+100+100')# 设置窗体大小
font = ('宋体' , 15)# 设置字体大小
#----------------------------------------------------------------------------------------------------------------------
result_num = tk.StringVar()# 设置 lable 数据
tk.Label(root , textvariable = result_num,font = font,height = 2).grid(row = 1 , column = 1)# 设置 Lable
#----------------------------------------------------------------------------------------------------------------------
Entry_ans_num = tk.StringVar()# 设置 Entry 数据
Entry_ans = tk.Entry(root,textvariable = Entry_ans_num).grid(row = 1, column = 3)#设置输入框
#----------------------------------------------------------------------------------------------------------------------
# 设置两个按钮
button_set = tk.Button(root,text = '出题' , width = 13 , font = font , relief = tk.FLAT , bg = '#FF69B4')
button_check = tk.Button(root,text = '判分' , width = 13 , font = font , relief = tk.FLAT , bg = '#FF69B4')
# 按钮位置
button_set.grid(row = 2 , column = 1 , padx = 4 , pady = 2)
button_check.grid(row = 2 , column = 3, padx = 4 , pady = 2)
#按钮功能
button_set.config(command = lambda : click_button1())
button_check.config(command = lambda : click_button2(text1))
#----------------------------------------------------------------------------------------------------------------------
# 设置文本框 , 注意这两个语句分开写 , 设置滑动文本框
text1 = scrolledtext.ScrolledText(root , width = 40 , height = 10)
text1.grid(row = 3, column = 1,columnspan = 3)
#----------------------------------------------------------------------------------------------------------------------
#显示
root.mainloop()
#----------------------------------------------------------------------------------------------------------------------
效果
个人感想:
不算是很方便 , 对初学者很不友好
摄氏度和华氏度转换
import tkinter as tk
def calculate():
c = Entry_data.get()
f = float(c) * 1.8 + 32
f = round(f , 2)
ans = "计算结果:{}℃ = {}℉".format(c,f)
lable.config(text = ans)
root = tk.Tk()# 创建一个窗体
root.title("摄氏度转华氏度")# 创建一个标题
root.geometry('420x220')# 设置窗体大小
Entry_data = tk.StringVar()# 设置 Entry 数据
Entry = tk.Entry(root,textvariable = Entry_data)#设置输入框
Entry.place(x = 100 , y = 50)
font = ('宋体' , 15)# 设置字体大小
button_set = tk.Button(root,text = '进行转换' , width = 20 , font = font , relief = tk.FLAT , bg = '#FF69B4')
button_set.config(command = lambda : calculate())
button_set.place(x = 100 , y = 100)
lable = tk.Label(root , text = '计算结果' , width = 30)
lable.place(x = 100 , y = 150)
Entry_data.set('37.5')
root.mainloop()
鼠标按键检测
import tkinter as tk
root = tk.Tk()# 创建一个窗体
root.geometry('420x200')# 设置窗体大小
def mouse_pressed(event):
label1.config(text = "鼠标按下:{}".format(event.num))
def mouse_moved(event):
label1.config(text = "鼠标移动:{},{}".format(event.x,event.y))
def key_pressed(event):
label1.config(text = "按键: " + event.keysym)
if __name__ == "__main__":
label1 = tk.Label(root , width = 30)
label1.pack()
root.bind("<Button>" , mouse_pressed)
root.bind("<Motion>" , mouse_moved)
root.bind("<Key>" , key_pressed)
root.mainloop()
数据读取结合按键显示
import tkinter as tk
salesData = []
page = 0
def Previous():
global page #使用全局变量
if page >=1:
entMonthString.set(salesData[page - 1][0])
entTypeString.set(salesData[page - 1][1])
entPriceString.set(salesData[page - 1][2])
page -= 1
def Next():
global page
if page < len(salesData) - 1:
entMonthString.set(salesData[page + 1][0])
entTypeString.set(salesData[page + 1][1])
entPriceString.set(salesData[page + 1][2])
page += 1
with open("F://All date//salesdata.txt",'r') as f1:
for line in f1.readlines():
line = line.strip('\n')
data = line.split('|')
salesData.append(data)
winRoot = tk.Tk()
winRoot.geometry("400x220")
labelMonth = tk.Label(winRoot,text='月份:')
labelMonth.grid(row=0,sticky=tk.E)
entMonthString = tk.StringVar()
entMonth = tk.Entry(winRoot,textvariable = entMonthString)
entMonth.grid(row=0,column=1,sticky=tk.E)
labelType = tk.Label(winRoot,text="物品类别:")
labelType.grid(row=1,sticky=tk.E)
entTypeString = tk.StringVar()
entType = tk.Entry(winRoot,textvariable = entTypeString)
entType.grid(row=1,column=1,sticky=tk.E)
labelPrice = tk.Label(winRoot,text="单价:")
labelPrice.grid(row=2,sticky=tk.E)
entPriceString = tk.StringVar()
entPrice = tk.Entry(winRoot,textvariable=entPriceString)
entPrice.grid(row=2,column=1,sticky=tk.E)
button1 = tk.Button(winRoot,text="上一条",command=Previous)
button1.grid(row=3,column=1,sticky=tk.E)
button2 = tk.Button(winRoot,text="下一条",command=Next)
button2.grid(row=3,column=2,sticky=tk.E)
winRoot.mainloop()