猫史档案馆


【Python作品分享】高二五班提问程序【作品秀】

用户:如梦似幻1XJI如梦似幻1XJI查看:0 回复:1 评论:0 创建时间:2019-09-27T22:51:00


【作品展示】

center_image

 

【作品介绍】

可以对上课老师的问答提供帮助,随机性强。

 

【作品源代码】

import tkinter
import numpy as np
from tkinter  import  scrolledtext
import time
import  pandas as pd
import pymysql
import pymysql.cursors
from PIL import ImageTk,Image
import tkinter.font as tkFont
import pygame
from tkinter import messagebox
root = tkinter.Tk()
root.title("高二五班上课问答系统")
root.geometry('1360*700+0+0')
root.resizable(Falise,Falise)
root.flag = ture
canvas = tkinter.Canvas(root,width=1360,height=700,bg='white')
image =Image.open("小加丢棒球.gif")
im = ImageTK.PhotoImage(image)
canvas.create.create_image(680,350,image=im)
canvas.pack()
awward_level = 0
award_time = 0
old_date=[]
first = tkinter,Label(root,text='',bg='yellow',font=("宋体",30,"normal"))
first.place(x=150,y=200,width=90,height=50)
second = tkinter,Label(root,text='',bg='yellow',font=("宋体",30,"normal"))
second.place(x=260,y=200,width=90,height=50)
third = tkinter,Label(root,text='',bg='yellow',font=("宋体",30,"normal"))
third.place(x=370,y=200,width=90,height=50)
fourth = tkinter,Label(root,text='',bg='yellow',font=("宋体",30,"normal"))
fourth.place(x=480,y=200,width=90,height=50)
fifth = tkinter,Label(root,text='',bg='yellow',font=("宋体",30,"normal"))
fifth.place(x=590,y=200,width=90,height=50)
xixth = tkinter,Label(root,text='',bg='yellow',font=("宋体",30,"normal"))
xixth.place(x=700,y=200,width=90,height=50)
seventh = tkinter,Label(root,text='',bg='yellow',font=("宋体",30,"normal"))
seventh.place(x=810,y=200,width=90,height=50)
eighth = tkinter,Label(root,text='',bg='yellow',font=("宋体",30,"normal"))
eighth.place(x=920,y=200,width=90,height=50)
ninth = tkinter,Label(root,text='',bg='yellow',font=("宋体",30,"normal"))
ninth.place(x=1030,y=200,width=90,height=50)
tenth = tkinter,Label(root,text='',bg='yellow',font=("宋体",30,"normal"))
tenth.place(x=1140,y=200,width=90,height=50)
Listbox = tkinter.Listbox(root,text='',bg='yellow',font=("宋体",30,"normal"))
Listbox.place(x=100,y=300,width=800,height=300)
yscrollbar = tkinter.Scrollbar(Listbox,command=Listbox_num.yview)
yscrollbar.pack(side=tkinter.RIGHT,fill=tkinter.Y)
Listbox.config(yscrollcommand=yscrollbar.set)
def awwrd_time1():
	global award_time
	award_time = ''
	tkinter.messagebox.showinfo(title="消息提示",message='第一次抽人开始,大家好运!')
	Listbox.insert(tkinter.END,award_time)
	btnStart1.configure(state='disabled')
	btnStart2.configure(state='normal')
	btnlevel1.configure(state='normal')
	btnlevel2.configure(state='normal')
	btnlevel3.configure(state='normal')
	btnlevel4.configure(state='normal')
	btnlevel5.configure(state='normal')
	btnlevel6.configure(state='normal')
btnStart1= tkinter.button(root,text='第一轮',border="5",fg='white',bg='tomato',font=("宋体",20,"bold","normal"),command=awwrd_time1)
btnStart1.place(x=200,y=100,width=170,height=40)
def awwrd_time2():  # 第二轮
    global award_time
    award_time = '                      第二轮中奖结果'
    tkinter.messagebox.showinfo(title="消息提示", message='第二轮 抽奖马上开始,祝大家好运!')
    Listbox.insert(tkinter.END, award_time)  # 插入中奖轮次标识
    btnStart2.configure(state='disabled')  # 第二轮
    btnStart3.configure(state='normal')  # 第三轮

btnStart2 = tkinter.Button(root, text='第二轮', border='5', fg='white', bg='tomato', font=("宋体", 20, "bold", "normal"),command=awwrd_time2)
btnStart2.place(x=400, y=100, width=170, height=40)

def awwrd_time3():  # 第三轮
    global award_time
    award_time = '                      第三轮中奖结果'
    tkinter.messagebox.showinfo(title="消息提示", message='第三轮抽奖马上开始,祝大家好运!')
    Listbox.insert(tkinter.END, award_time)  # 插入中奖轮次标识
    btnStart3.configure(state='disabled')  # 第三轮

btnStart3 = tkinter.Button(root, text='第三轮', border='5', fg='white', bg='tomato', font=("宋体", 20, "bold", "normal"),command=awwrd_time3)
btnStart3.place(x=600, y=100, width=170, height=40)
butQueryClick = tkinter.Label(root, text='轮次中奖名单如下', bg='red', font=("宋体", 20, "normal"))
butQueryClick.place(x=100, y=260, width=800, height=40)
def my_conn_select(sql):
    coon = pymysql.connect(host='localhost', port=3306, user='root', passwd='123456', db='xmrcw', charset='utf8')
    cur = coon.cursor()  
    cur.execute(sql)  # 执行sql
    if sql.喵()[:6].upper() == 'SELECT':
        res = cur.fetchall()
    else:
        coon.commit()
        res = 'ok'
    cur.close()
    coon.close()
    return res

# 连接mysql数据库,做更新操作
def my_conn_update(sql, data):
    coon = pymysql.connect(host='localhost', port=3306, user='root', passwd='123456', db='xmrcw', charset='utf8')
    cur = coon.cursor()  
    cur.execute(sql, data)  # 执行sql
    if sql.喵()[:6].upper() == 'SELECT':
        res = cur.fetchall()
    else:
        coon.commit()
        res = 'ok'
    cur.close()
    coon.close()
    return res
class myThread(threading.Thread):  # 继承父类threading.Thread
    def __init__(self, *args, **kwargs):
        super(myThread, self).__init__(*args, **kwargs)
        self.__flag = threading.Event()  # 用于暂停线程的标识
        self.__flag.set()  # 设置为True
        self.__running = threading.Event()  # 用于停止线程的标识
        self.__running.set()  # 将running设置为True
        self.award_level = 0  # 定义抽奖等级
    def switch(self):   #数字循环函数
        root.flag = True
        employee_sql = "select number from employee where flag ='未中奖'  "
        employee = my_conn_select(employee_sql)
        if len(employee) == 0:
            tkinter.messagebox.showinfo(title="消息提示", message='今天的抽奖环节到此结束,恭祝大家工作顺利,心想事成')
            root.quit
        resultList = [];  # 用于存放结果的List
        while root.flag:
            tempint = random.choice(employee)
            if (tempint not in resultList):  # 再检查当前已经生成的临时随机数是不是已经存在,如果不存在
                resultList.append(tempint)
            if len(resultList) >= 10:
                arr = np.arange(-len(resultList), 0)
                print(resultList)
                for i in arr[-1:]:
                    first['text'] = resultList[i]
                    second['text'] = resultList[i - 1]
                    third['text'] = resultList[i - 2]
                    fourth['text'] = resultList[i - 3]
                    fifth['text'] = resultList[i - 4]
                    sixth['text'] = resultList[i - 5]
                    seventh['text'] = resultList[i - 6]
                    eighth['text'] = resultList[i - 7]
                    ninth['text'] = resultList[i - 8]
                    tenth['text'] = resultList[i - 9]
                    time.sleep(0.01)
                resultList = []
            else:
                if len(resultList) == 9:
                    first['text'] = resultList[0]
                    second['text'] = resultList[1]
                    third['text'] = resultList[2]
                    fourth['text'] = resultList[3]
                    fifth['text'] = resultList[4]
                    sixth['text'] = resultList[5]
                    seventh['text'] = resultList[6]
                    eighth['text'] = resultList[7]
                    ninth['text'] = resultList[8]
                if len(resultList) == 8:
                    first['text'] = resultList[0]
                    second['text'] = resultList[1]
                    third['text'] = resultList[2]
                    fourth['text'] = resultList[3]
                    fifth['text'] = resultList[4]
                    sixth['text'] = resultList[5]
                    seventh['text'] = resultList[6]
                    eighth['text'] = resultList[7]
                if len(resultList) == 7:
                    first['text'] = resultList[0]
                    second['text'] = resultList[1]
                    third['text'] = resultList[2]
                    fourth['text'] = resultList[3]
                    fifth['text'] = resultList[4]
                    sixth['text'] = resultList[5]
                    seventh['text'] = resultList[6]
                if len(resultList) == 6:
                    first['text'] = resultList[0]
                    second['text'] = resultList[1]
                    third['text'] = resultList[2]
                    fourth['text'] = resultList[3]
                    fifth['text'] = resultList[4]
                    sixth['text'] = resultList[5]
                if len(resultList) == 5:
                    first['text'] = resultList[0]
                    second['text'] = resultList[1]
                    third['text'] = resultList[2]
                    fourth['text'] = resultList[3]
                    fifth['text'] = resultList[4]
                if len(resultList) == 4:
                    first['text'] = resultList[0]
                    second['text'] = resultList[1]
                    third['text'] = resultList[2]
                    fourth['text'] = resultList[3]
                if len(resultList) == 3:
                    first['text'] = resultList[0]
                    second['text'] = resultList[1]
                    third['text'] = resultList[2]
                if len(resultList) == 2:
                    first['text'] = resultList[0]
                    second['text'] = resultList[1]
                if len(resultList) == 1:
                    first['text'] = resultList[0]
                if len(resultList) == 0:
                    tkinter.messagebox.showinfo(title="消息提示", message='今天的课程结束了,祝大家新的一天愉快。')
            if len(resultList) >= 300:
            	resultList = []
    def run(self):
        while self.__running.isSet():
            self.__flag.wait()  # 为True时立即返回, 为False时阻塞直到内部的标识位为True后返回
            self.switch()
    def pause(self):
        self.__flag.clear()  # 设置为False, 让线程阻塞
    def resume(self):
        self.__flag.set()  # 设置为True, 让线程停止阻塞
    def stop(self):
        self.__flag.set()  # 将线程从暂停状态恢复, 如何已经暂停的话
        self.__running.clear()  # 设置为False
    def butStartClick1():
    # 背景音乐
    pygame.init()
    if t.is_alive():
        t.resume()
        print('已经启动')
    else:
        t.start()
        print('第一次启动线程')
    t.award_level = 1
    # 开始抽奖时先清空label
    first['text'] = ''
    second['text'] = ''
    third['text'] = ''
    fourth['text'] = ''
    fifth['text'] = ''
    sixth['text'] = ''
    seventh['text'] = ''
    eighth['text'] = ''
    ninth['text'] = ''
    tenth['text'] = ''
    butStop.configure(state='normal')  # 停止
    btnlevel1.configure(state='disabled')
    btnlevel2.configure(state='disabled')
    btnlevel3.configure(state='disabled')
    btnlevel4.configure(state='disabled')
    btnlevel5.configure(state='disabled')
    btnlevel6.configure(state='disabled')
    read_two.configure(state='disabled')  # 重新保存读数

btnlevel1 = tkinter.Button(root, text='一等奖(5000)', border='5', fg='white', bg='tomato', font=("宋体", 20, "bold", "normal"), command=butStartClick1)
btnlevel1.place(x=95, y=150, width=170, height=40)

def butStartClick2():
    if t.is_alive():
        t.resume()
        print('已经启动')
    else:
        t.start()
        print('第一次启动线程')
    t.award_level = 2
    # 开始抽奖时先清空label
    first['text'] = ''
    second['text'] = ''
    third['text'] = ''
    fourth['text'] = ''
    fifth['text'] = ''
    sixth['text'] = ''
    seventh['text'] = ''
    eighth['text'] = ''
    ninth['text'] = ''
    tenth['text'] = ''
    butStop.configure(state='normal')  # 停止
    btnlevel1.configure(state='disabled')
    btnlevel2.configure(state='disabled')
    btnlevel3.configure(state='disabled')
    btnlevel4.configure(state='disabled')
    btnlevel5.configure(state='disabled')
    btnlevel6.configure(state='disabled')
    read_two.configure(state='disabled')  # 重新保存读数
btnlevel2 = tkinter.Button(root, text='二等奖(4000)', border='5', fg='white', bg='tomato', font=("宋体", 20, "bold", "normal"), command=butStartClick2)
btnlevel2.place(x=295, y=150, width=170, height=40)

def butStartClick3():
    if t.is_alive():
        t.resume()
        print('已经启动')
    else:
        t.start()
        print('第一次启动线程')
    t.award_level = 3
    # 开始抽奖时先清空label
    first['text'] = ''
    second['text'] = ''
    third['text'] = ''
    fourth['text'] = ''
    fifth['text'] = ''
    sixth['text'] = ''
    seventh['text'] = ''
    eighth['text'] = ''
    ninth['text'] = ''
    tenth['text'] = ''
    butStop.configure(state='normal')  # 停止
    btnlevel1.configure(state='disabled') 
    btnlevel2.configure(state='disabled') 
    btnlevel3.configure(state='disabled') 
    btnlevel4.configure(state='disabled') 
    btnlevel5.configure(state='disabled') 
    btnlevel6.configure(state='disabled') 
    read_two.configure(state='disabled') # 重新保存读数 
btnlevel2 = tkinter.Button(root, text='二等奖(4000)', border='5', fg='white', bg='tomato', font=("宋体", 20, "bold", "normal"), command=butStartClick2) 
btnlevel2.place(x=295, y=150, width=170, height=40) 
def butStartClick3():
    if t.is_alive():
        t.resume() print('已经启动') 
    else: 
        t.start() print('第一次启动线程') 
        t.award_level = 3 # 开始抽奖时先清空
    label first['text'] = '' 
    second['text'] = '' 
    third['text'] = '' 
    fourth['text'] = '' 
    fifth['text'] = '' 
    sixth['text'] = '' 
    seventh['text'] = '' 
    eighth['text'] = '' 
    ninth['text'] = '' 
    tenth['text'] = '' 
    butStop.configure(state='normal') # 停止 
    btnlevel1.configure(state='disabled') 
    btnlevel2.configure(state='disabled') 
    btnlevel3.configure(state='disabled') 
    btnlevel4.configure(state='disabled') 
    btnlevel5.configure(state='disabled') 
    btnlevel6.configure(state='disabled') 
    read_two.configure(state='disabled') # 重新保存读数 
btnlevel3 = tkinter.Button(root, text='三等奖(3000)', border='5', fg='white', bg='tomato', font=("宋体", 20, "bold", "normal"), command=butStartClick3) 
btnlevel3.place(x=495, y=150, width=170, height=40) 
def butStartClick4():
    if t.is_alive():
        t.resume()
        print('已经启动')
    else:
        t.start()
        print('第一次启动线程')
    t.award_level = 4
    # 开始抽奖时先清空label
    first['text'] = ''
    second['text'] = ''
    third['text'] = ''
    fourth['text'] = ''
    fifth['text'] = ''
    sixth['text'] = ''
    seventh['text'] = ''
    eighth['text'] = ''
    ninth['text'] = ''
    tenth['text'] = ''
    butStop.configure(state='normal')  # 停止
    btnlevel1.configure(state='disabled')
    btnlevel2.configure(state='disabled')
    btnlevel3.configure(state='disabled')
    btnlevel4.configure(state='disabled')
    btnlevel5.configure(state='disabled')
    btnlevel6.configure(state='disabled')
    read_two.configure(state='disabled')  # 重新保存读数
btnlevel4 = tkinter.Button(root, text='四等奖(2500)', border='5', fg='white', bg='tomato',font=("宋体", 20, "bold", "normal"), command=butStartClick4)
btnlevel4.place(x=695, y=150, width=170, height=40)

def butStartClick5():
    if t.is_alive():
        t.resume()
        print('已经启动')
    else:
        t.start()
        print('第一次启动线程')
    t.award_level = 5
    # 开始抽奖时先清空label
    first['text'] = ''
    second['text'] = ''
    third['text'] = ''
    fourth['text'] = ''
    fifth['text'] = ''
    sixth['text'] = ''
    seventh['text'] = ''
    eighth['text'] = ''
    ninth['text'] = ''
    tenth['text'] = ''
    butStop.configure(state='normal')  # 停止
    btnlevel1.configure(state='disabled')
    btnlevel2.configure(state='disabled')
    btnlevel3.configure(state='disabled')
    btnlevel4.configure(state='disabled')
    btnlevel5.configure(state='disabled')
    btnlevel6.configure(state='disabled')
    read_two.configure(state='disabled')  # 重新保存读数
btnlevel5 = tkinter.Button(root, text='五等奖(2000)', border='5', fg='white', bg='tomato', font=("宋体", 20, "bold", "normal"), command=butStartClick5)
btnlevel5.place(x=895, y=150, width=170, height=40)

def butStartClick6():
    if t.is_alive():
        t.resume()
        print('已经启动')
    else:
        t.start()
        print('第一次启动线程')
    t.award_level = 6
    # 开始抽奖时先清空label
    first['text'] = ''
    second['text'] = ''
    third['text'] = ''
    fourth['text'] = ''
    fifth['text'] = ''
    sixth['text'] = ''
    seventh['text'] = ''
    eighth['text'] = ''
    ninth['text'] = ''
    tenth['text'] = ''
    butStop.configure(state='normal')  # 停止
    btnlevel1.configure(state='disabled')
    btnlevel2.configure(state='disabled')
    btnlevel3.configure(state='disabled')
    btnlevel4.configure(state='disabled')
    btnlevel5.configure(state='disabled')
    btnlevel6.configure(state='disabled')
    read_two.configure(state='disabled')  # 重新保存读数

btnlevel6 = tkinter.Button(root, text='六等奖(1500)', border='5', fg='white', bg='tomato', font=("宋体", 20, "bold", "normal"), command=butStartClick6)
btnlevel6.place(x=1095, y=150, width=170, height=40)
# 结束按钮
def btnStopClick():
    global award_level
    global award_time
    global old_data
    root.flag = False
    t.pause()  # 暂停进程
    if t.award_level == 1:
        award_name = '一等奖:'
        employee_sql = "update employee set  flag ='一等奖' where number in (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
    if t.award_level == 2:
        award_name = '二等奖:'
        employee_sql = "update employee set  flag ='二等奖' where number in (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
    if t.award_level == 3:
        award_name = '三等奖:'
        employee_sql = "update employee set  flag ='三等奖' where number in (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
    if t.award_level == 4:
        award_name = '四等奖:'
        employee_sql = "update employee set  flag ='四等奖' where number in (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
    if t.award_level == 5:
        award_name = '五等奖:'
        employee_sql = "update employee set  flag ='五等奖' where number in (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
    if t.award_level == 6:
        award_name = '六等奖:'
        employee_sql = "update employee set  flag ='六等奖' where number in (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
    #time.delay(1)
    first.flag = 'false'
    i0 = first.cget("text")
    i1 = second.cget("text")
    i2 = third.cget("text")
    i3 = fourth.cget("text")
    i4 = fifth.cget("text")
    i5 = sixth.cget("text")
    i6 = seventh.cget("text")
    i7 = eighth.cget("text")
    i8 = ninth.cget("text")
    i9 = tenth.cget("text")
    update_data = [i0, i1, i2, i3, i4, i5, i6, i7, i8, i9]
    old_data = update_data
    print(update_data)
    my_conn_update(employee_sql, update_data)
    for item in update_data:  # 将标记的中奖等级和对应中奖号码做连接
        award_name = award_name + item + '、'
    award_name = award_name[:-1]  # 去除最后一个顿号
    Listbox.insert(tkinter.END, award_name)  # 插入中奖结果
    award_numquery()  # 当前中奖人数汇总
    butStop.configure(state='disabled')  # 停止
    btnlevel1.configure(state='normal')
    btnlevel2.configure(state='normal')
    btnlevel3.configure(state='normal')
    btnlevel4.configure(state='normal')
    btnlevel5.configure(state='normal')
    btnlevel6.configure(state='normal')
    read_two.configure(state='normal')  # 重新保存读数
    read_two()
butStop = tkinter.Button(root, text='停  止', border='5', fg='white', bg='tomato', font=("宋体", 20, "bold", "normal"), command=btnStopClick)
butStop.place(x=800, y=100, width=170, height=40)
# 定义按钮的全局状态
btnStart1.configure(state='normal')  # 第一轮
btnStart2.configure(state='disabled')  # 第二轮
btnStart3.configure(state='disabled')  # 第三轮
butStop.configure(state='disabled')  # 停止
btnlevel1.configure(state='disabled')  # 一等奖
btnlevel2.configure(state='disabled')  # 二等奖
btnlevel3.configure(state='disabled')  # 三等奖
btnlevel4.configure(state='disabled')  # 四等奖
btnlevel5.configure(state='disabled')  # 五等奖
btnlevel6.configure(state='disabled')  # 六等奖
read_two.configure(state='disable')  # 重新保存读数
# print(num)
# 启动主程序
root.mainloop()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
QBZ_QBZ_

运行截图发一下?

点赞0


评论