我正在构建一个 Python 应用程序,我一直在考虑一个问题。Python 并非在所有设备上都可用,更不用说我使用的模块了。任何想法如何将解释器和模块嵌入到 .exe 文件中?如果我没记错的话,minecraft 本身在安装过程中会导入 Java。
主页
/
user-496974
retr 12's questions
我正在 tkinter 上制作应用程序,我想将图片作为背景以及所有其他按钮和标签放在上面。有些东西对我不起作用,图片与其他所有内容重叠。下面是代码:
root = Tk()
root.geometry('1920x1080')
root.title('QuizOp')
root['bg'] = 'limegreen'
generalframe = Frame(master=root, bg='limegreen')
bgimage = ImageTk.PhotoImage(Image.open("stestimage.jpg"))
bglabel = Label(root, image=bgimage)
createquizbtn = Button(generalframe, bg='white', text='Создать викторину', font='Arial 17', command=createquiz)
proitiquizbtn = Button(generalframe, bg='white', text='Пройти викторину', font='Arial 17', command=proitiquiz)
sendquizbtn = Button(generalframe, bg='white', text='Отправить викторину', font='Arial 17', command=sendquiz)
deletequizbtn = Button(generalframe, bg='white', text='Удалить викторину', font='Arial 17', command=deletequiz)
settingsbtn = Button(generalframe, bg='white', text='Настройки', font='Arial 17', command=settingscmd)
bglabel.pack()
generalframe.pack()
createquizbtn.pack()
proitiquizbtn.pack()
sendquizbtn.pack()
deletequizbtn.pack()
settingsbtn.pack()
root.mainloop()
如何用另一个非嵌套函数停止一个函数?我试图用return来停止这个函数,但它没有出来。vopr2var1btnfunc 函数通过按钮调用。我想用 vopr2var1btnfunc 停止 update_time1
def vopr1var1btnfunc():
variant1btn.pack_forget()
vopr1var1lbl.pack()
vopr1var1ent.pack()
vopr1var1btn.pack()
global currentvariant
currentvariant = 'Задания для 5-6 классов'
vopr1timerlbl.pack(anchor=CENTER)
format1 = '%H:%M:%S'
target1 = datetime.now() + timedelta(minutes=2)
global update_time1
def update_time1():
now = datetime.now()
if now < target1:
remains = round((target1 - now).total_seconds())
text = time(second=remains % 60, minute=remains // 60).strftime(format1)
vopr1timerlbl.config(text=text)
vopr1timerlbl.after(1000, update_time1)
else:
vopr2var1()
update_time1()
def vopr2var1btnfunc():
if update_time1():
return
global vopr1var1answer
vopr1var1answer = vopr1var1ent.get()
vopr1var1lbl.pack_forget()
vopr1var1ent.pack_forget()
vopr1var1btn.pack_forget()
vopr1timerlbl.pack_forget()
vopr2var1lbl.pack()
vopr2var1ent.pack()
vopr2var1btn.pack()
vopr2timerlbl.pack(anchor=CENTER)
format2 = '%H:%M:%S'
target2 = datetime.now() + timedelta(minutes=2)
global update_time2
def update_time2():
now = datetime.now()
if now < target2:
remains = round((target2 - now).total_seconds())
text = time(second=remains % 60, minute=remains // 60).strftime(format2)
vopr2timerlbl.config(text=text)
vopr2timerlbl.after(1000, update_time2)
else:
vopr3var1()
update_time2()
我想做一个关于奥运会的测试程序,但是我有一个问题。我正在努力做到这一点,以便在计时器结束时过渡到下一个问题。但是发生错误并且计时器几乎没有返回或没有消失。我将代码减少到有问题的部分。我已经尝试以许多不同的方式将 return 作为函数、条件语句。
这是代码:
from tkinter import messagebox
from _tkinter import *
import os
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from platform import python_version
from datetime import datetime, time, timedelta
root = Tk()
root.geometry('1000x800')
root['bg'] = 'cyan4'
root.title('Quizop')
teamnamelbl = Label(root, bg='skyblue1', text='Введите название команды', font='Arial 18', width=43)
teamnameent = Entry(root, bg='white', justify=CENTER, width=47, font='Arial 18')
captainnamelbl = Label(root, bg='skyblue1', text='Введите капитана', font='Arial 18')
captainnameent = Entry(root, bg='white', justify=CENTER, width=47, font='Arial 18')
member2namelbl = Label(root, bg='skyblue1', text='Введите второго участника команды', font='Arial 18')
member2nameent = Entry(root, bg='white', justify=CENTER, width=47, font='Arial 18')
member3namelbl = Label(root, bg='skyblue1', text='Введите третьего участника команды', font='Arial 18')
member3nameent = Entry(root, bg='white', justify=CENTER, width=47, font='Arial 18')
member4namelbl = Label(root, bg='skyblue1', text='Введите четвертого участника команды', font='Arial 18')
member4nameent = Entry(root, bg='white', justify=CENTER, width=47, font='Arial 18')
member5namelbl = Label(root, bg='skyblue1', text='Введите пятого участника команды', font='Arial 18')
member5nameent = Entry(root, bg='white', justify=CENTER, width=47, font='Arial 18')
member6namelbl = Label(root, bg='skyblue1', text='Введите шестого участника команды', font='Arial 18')
member6nameent = Entry(root, bg='white', justify=CENTER, width=47, font='Arial 18')
vopr1var1lbl = Label(root, bg='skyblue1', text='Назовите родину олимпийских игр', font='Arial 18')
vopr1var1ent = Entry(root, bg='white', justify=CENTER, font='Arial 15', width=35)
vopr1timerlbl = Label(root, font=('calibri', 50, 'bold'), background='skyblue1', foreground='black')
vopr2var1lbl = Label(root, bg='skyblue1', text='Кем были учреждены олимпийские игры?', font='Arial 18')
vopr2var1ent = Entry(root, bg='white', justify=CENTER, font='Arial 15', width=35)
vopr2timerlbl = Label(root, font=('calibri', 50, 'bold'), background='skyblue1', foreground='black')
vopr3var1lbl = Label(root, bg='skyblue1', text='В честь какого бога устраивались олимпийские игры?', font='Arial 18')
vopr3var1ent = Entry(root, bg='white', justify=CENTER, font='Arial 15', width=35)
vopr3timerlbl = Label(root, font=('calibri', 50, 'bold'), background='skyblue1', foreground='black')
vopr4var1lbl = Label(root, bg='skyblue1', text='Кто не мог участвовать в олимпийских играх в древности?', font='Arial 18')
vopr4var1ent = Entry(root, bg='white', justify=CENTER, font='Arial 15', width=35)
vopr4timerlbl = Label(root, font=('calibri', 50, 'bold'), background='skyblue1', foreground='black')
vopr5var1lbl = Label(root, bg='skyblue1', text='В каком году состоялись первые олимпийские игры?', font='Arial 18')
vopr5var1ent = Entry(root, bg='white', justify=CENTER, font='Arial 15', width=35)
vopr5timerlbl = Label(root, font=('calibri', 50, 'bold'), background='skyblue1', foreground='black')
vopr6var1lbl = Label(root, bg='skyblue1', text='В каком городе состоялись первые олимпийские игры?', font='Arial 18')
vopr6var1ent = Entry(root, bg='white', justify=CENTER, font='Arial 15', width=35)
vopr6timerlbl = Label(root, font=('calibri', 50, 'bold'), background='skyblue1', foreground='black')
vopr7var1lbl = Label(root, bg='skyblue1', text='Участники соревнований в древности?', font='Arial 18')
vopr7var1ent = Entry(root, bg='white', justify=CENTER, font='Arial 15', width=35)
vopr7timerlbl = Label(root, font=('calibri', 50, 'bold'), background='skyblue1', foreground='black')
vopr8var1lbl = Label(root, bg='skyblue1', text='Опишите 1-ый день соревнований.', font='Arial 18')
vopr8var1ent = Entry(root, bg='white', justify=CENTER, font='Arial 15', width=35)
vopr8timerlbl = Label(root, font=('calibri', 50, 'bold'), background='skyblue1', foreground='black')
vopr9var1lbl = Label(root, bg='skyblue1', text='Опишите 2-ой день соревнований.', font='Arial 18')
vopr9var1ent = Entry(root, bg='white', justify=CENTER, font='Arial 15', width=35)
vopr9timerlbl = Label(root, font=('calibri', 50, 'bold'), background='skyblue1', foreground='black')
vopr10var1lbl = Label(root, bg='skyblue1', text='Опишите 3-ий день соревнований.', font='Arial 18')
vopr10var1ent = Entry(root, bg='white', justify=CENTER, font='Arial 15', width=35)
vopr10timerlbl = Label(root, font=('calibri', 50, 'bold'), background='skyblue1', foreground='black')
vopr11var1lbl = Label(root, bg='skyblue1', text='Опишите 4-ый день соревнований.', font='Arial 18')
vopr11var1ent = Entry(root, bg='white', justify=CENTER, font='Arial 15', width=35)
vopr11timerlbl = Label(root, font=('calibri', 50, 'bold'), background='skyblue1', foreground='black')
vopr12var1lbl = Label(root, bg='skyblue1', text='Опишите 5-ый день соревнований.', font='Arial 18')
vopr12var1ent = Entry(root, bg='white', justify=CENTER, font='Arial 15', width=35)
vopr12timerlbl = Label(root, font=('calibri', 50, 'bold'), background='skyblue1', foreground='black')
vopr13var1lbl = Label(root, bg='skyblue1', text='Кто такой Милон?', font='Arial 18')
vopr13var1ent = Entry(root, bg='white', justify=CENTER, font='Arial 15', width=35)
vopr13timerlbl = Label(root, font=('calibri', 50, 'bold'), background='skyblue1', foreground='black')
finalrootlbl = Label(root, bg='skyblue1', text='Поздравляем! Вы завершили тест!', font='Arial 18')
format = '%H:%M:%S'
def startcommand():
global teamnamestr
teamnamestr = str(teamnameent.get())
global captainnamestr
captainnamestr = str(captainnameent.get())
global member2namestr
member2namestr = str(member2nameent.get())
global member3namestr
member3namestr = str(member3nameent.get())
global member4namestr
member4namestr = str(member4nameent.get())
global member5namestr
member5namestr = str(member5nameent.get())
global member6namestr
member6namestr = str(member6nameent.get())
teamnamelbl.pack_forget()
teamnameent.pack_forget()
captainnamelbl.pack_forget()
captainnameent.pack_forget()
member2namelbl.pack_forget()
member2nameent.pack_forget()
member3namelbl.pack_forget()
member3nameent.pack_forget()
member4namelbl.pack_forget()
member4nameent.pack_forget()
member5namelbl.pack_forget()
member5nameent.pack_forget()
member6namelbl.pack_forget()
member6nameent.pack_forget()
startbtn.pack_forget()
variant1btn.pack()
def vopr1var1btnfunc():
variant1btn.pack_forget()
vopr1var1lbl.pack()
vopr1var1ent.pack()
vopr1var1btn.pack()
global currentvariant
currentvariant = 'Задания для 5-6 классов'
vopr1timerlbl.pack(anchor=CENTER)
format1 = '%H:%M:%S'
target1 = datetime.now() + timedelta(minutes=2)
global update_time1
def update_time1():
now = datetime.now()
if now < target1:
remains = round((target1 - now).total_seconds())
text = time(second=remains % 60, minute=remains // 60).strftime(format1)
vopr1timerlbl.config(text=text)
else:
vopr2var1()
vopr1timerlbl.after(1000, update_time1)
update_time1()
def vopr2var1btnfunc():
if update_time1():
return
global vopr1var1answer
vopr1var1answer = vopr1var1ent.get()
vopr1var1lbl.pack_forget()
vopr1var1ent.pack_forget()
vopr1var1btn.pack_forget()
vopr1timerlbl.pack_forget()
vopr2var1lbl.pack()
vopr2var1ent.pack()
vopr2var1btn.pack()
vopr2timerlbl.pack(anchor=CENTER)
format2 = '%H:%M:%S'
target2 = datetime.now() + timedelta(minutes=2)
global update_time2
def update_time2():
now = datetime.now()
if now < target2:
remains = round((target2 - now).total_seconds())
text = time(second=remains % 60, minute=remains // 60).strftime(format2)
vopr2timerlbl.config(text=text)
else:
vopr3var1()
vopr2timerlbl.after(1000, update_time2)
update_time2()
def vopr2var1():
global vopr1var1answer
vopr1var1answer = vopr1var1ent.get()
vopr1var1lbl.pack_forget()
vopr1var1ent.pack_forget()
vopr1var1btn.pack_forget()
vopr1timerlbl.pack_forget()
vopr2var1lbl.pack()
vopr2var1ent.pack()
vopr2var1btn.pack()
vopr2timerlbl.pack(anchor=CENTER)
format2 = '%H:%M:%S'
target2 = datetime.now() + timedelta(minutes=2)
global update_time2
def update_time2():
now = datetime.now()
if now < target2:
remains = round((target2 - now).total_seconds())
text = time(second=remains % 60, minute=remains // 60).strftime(format2)
vopr2timerlbl.config(text=text)
else:
vopr3var1()
vopr2timerlbl.after(1000, update_time2)
update_time2()
def vopr3var1btnfunc():
if update_time2():
return
global vopr2var1answer
vopr2var1answer = vopr2var1ent.get()
vopr2var1lbl.pack_forget()
vopr2var1ent.pack_forget()
vopr2var1btn.pack_forget()
vopr2timerlbl.pack_forget()
vopr3var1lbl.pack()
vopr3var1ent.pack()
vopr3var1btn.pack()
vopr3timerlbl.pack(anchor=CENTER)
format3 = '%H:%M:%S'
target3 = datetime.now() + timedelta(minutes=2)
global update_time3
def update_time3():
now = datetime.now()
if now < target3:
remains = round((target3 - now).total_seconds())
text = time(second=remains % 60, minute=remains // 60).strftime(format3)
vopr3timerlbl.config(text=text)
else:
vopr4var1()
vopr3timerlbl.after(1000, update_time3)
update_time3()
def vopr3var1():
global vopr2var1answer
vopr2var1answer = vopr2var1ent.get()
vopr2var1lbl.pack_forget()
vopr2var1ent.pack_forget()
vopr2var1btn.pack_forget()
vopr2timerlbl.pack_forget()
vopr3var1lbl.pack()
vopr3var1ent.pack()
vopr3var1btn.pack()
vopr3timerlbl.pack(anchor=CENTER)
format3 = '%H:%M:%S'
target3 = datetime.now() + timedelta(minutes=2)
global update_time3
def update_time3():
now = datetime.now()
if now < target3:
remains = round((target3 - now).total_seconds())
text = time(second=remains % 60, minute=remains // 60).strftime(format3)
vopr3timerlbl.config(text=text)
else:
vopr4var1()
vopr3timerlbl.after(1000, update_time3)
update_time3()
def vopr4var1btnfunc():
if update_time3():
return
global vopr3var1answer
vopr3var1answer = vopr3var1ent.get()
vopr3var1lbl.pack_forget()
vopr3var1ent.pack_forget()
vopr3var1btn.pack_forget()
vopr3timerlbl.pack_forget()
vopr4var1lbl.pack()
vopr4var1ent.pack()
vopr4var1btn.pack()
vopr4timerlbl.pack(anchor=CENTER)
format4 = '%H:%M:%S'
target4 = datetime.now() + timedelta(minutes=2)
global update_time4
def update_time4():
now = datetime.now()
if now < target4:
remains = round((target4 - now).total_seconds())
text = time(second=remains % 60, minute=remains // 60).strftime(format4)
vopr4timerlbl.config(text=text)
else:
vopr5var1()
vopr4timerlbl.after(1000, update_time4)
update_time4()
root.mainloop()```
尝试使用 Python Tkinter 创建一个 3 分钟倒数计时器。3 分钟后,窗口应关闭。发生错误,我不知道为什么。当窗口启动时,只有绿色背景上的蓝色条纹。
这是代码:
from tkinter import *
from datetime import datetime
from time import strptime, time
from tkinter import *
from datetime import datetime
from datetime import timedelta
root = Tk()
root.title('QuizopTimer')
root.geometry('1000x800')
root['bg'] = 'limegreen'
def update_time():
format = '%H:%M:%S'
s2 = (datetime.now() + timedelta(minutes=3)).strftime(format)
now = (datetime.now()).strftime(format)
s3 = datetime.strptime(s2, format) - datetime.strptime(now, format)
timerlbl.config(text=s3)
if not s3 == '00:00:00':
now2 = (datetime.now()).strftime(format)
s4 = datetime.strptime(s2, format) - datetime.strptime(now2, format)
timerlbl.config(text=s4)
else:
root.destroy()
timerlbl.after(1000, update_time)
timerlbl = Label(root, font=('calibri', 50, 'bold'), background='blue', foreground='white')
timerlbl.pack(anchor=CENTER)
root.mainloop()
update_time()
错误:
Возникло исключение: TclError
invalid command name ".!label"
File "C:\Games\quizopfortrn\timertest.py", line 18, in update_time
timerlbl.config(text=s3)
File "C:\Games\quizopfortrn\timertest.py", line 31, in <module>
update_time()