Python Tkiner 图形化设计作业记录(update 22.10.29)

要求

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()
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

每天都想发疯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值