[英]Problems with messagebox in tkinter
I am trying to create a jumble word game in python tkinter but I have difficulty trying to create a front window where the user can see the game rules etc. I tried creating a frame so I can display the information of the rules inside but the messagebox keeps popping up when running the code.我正在尝试在 python tkinter 中创建一个混乱的文字游戏,但我很难尝试创建一个用户可以看到游戏规则等的前窗口。我尝试创建一个框架,以便我可以在里面显示规则的信息,但在消息框运行代码时不断弹出。 Is there a way to fix that problem or create a first window that shows the rules which then redirects you to the game ?
有没有办法解决这个问题或创建一个显示规则的第一个窗口,然后将您重定向到游戏? Also I apologize if some of the words are in french in my code.
如果我的代码中的某些单词是法语,我也深表歉意。 Thank you.
谢谢你。
from tkinter import *
import random
from tkinter import messagebox
#Creation de la fenetre
fenetre = Tk()
rep =["python",'virus','while','for','if','label','import','tkinter','random','fenetre','uct','clavier','hertz','print','global']
mots=["ohtnpy",'visur','lehiw','rfo','fi','eblal','ripotm','kntiert','draomn','nefetre','tcu','vrilcea','heztr','tnrip','oabgll']
lettre =random.randrange(0,len(mots),1)
#La variable du score
a=0
#Le nombre de fois joué
j=0
#Variable qui calcule le score
s = ""
#L'affichage du score
lbl_score= Label(fenetre)
def mélanger():
global mots, rep, lettre
lettres =random.randrange(0,len(mots),1)
label.config(text = mots[lettres])
E1.delete(0, END)
def default():
global mots,rep,lettre
label.config(text = mots[lettre])
def vérif_rep():
global mots, rep,lettre, a, j, s, lbl_score
j=int(j)+1
trouve = E1.get() #obtenir la reponse
if trouve == rep[lettre]: #Trouve si la réponse est juste ou non
messagebox.showinfo("Bravo", "C'est la bonne reponse!!")
a = int(a)+1
else:
messagebox.showerror("Oops", "C'est pas la bonne réponse.")
s = 'Score :' + str(a) + '/' + str(j)
lbl_score.forget()
lbl_score = Label(fenetre, font=("Verdana", 20), text=s, bg="#800080", fg="#fff", )
lbl_score.pack(side=LEFT)
mélanger()
#Affichage de la fenètre
fenetre.geometry("800x500")
fenetre.title("Jeu de mots-melés")
fenetre.configure(background="light pink")
#Création du titre
Label(fenetre,text="Jeu de mots-melés",font = ("Verdana",28),bg = "#000000", fg = "#fff").pack(pady = 5)
label = Label(fenetre,font = ("Verdana",22),bg = "#000000", fg = "#fff")
label.pack(pady = 30,ipady=10,ipadx=10)
obt_rep = StringVar()
#Boite d'entrée
E1 = Entry(fenetre,font = ("Verdana",20),textvariable = obt_rep,)
E1.pack(ipady=5,ipadx=5)
#Les boutons du code
Button(fenetre,text = "Vérifier",font = ("Comic sans ms",20),width = 10,bg="#333945",fg="#45CE30",relief = GROOVE,command = vérif_rep(),).pack(pady = 40)
Button(fenetre,text = "Mélanger",font = ("Comic sans ms",20),width = 10,bg="#777E8B",fg="#E1DA00",relief = GROOVE,command = mélanger()).pack()
C3 = Frame(fenetre, relief='groove', bd=10)
C3.place(x=700, y=300, width=500, height=500)
default()
fenetre.mainloop()
Here is an example how to create two windows from the beginning or creating a windows with buttons.这是一个如何从头开始创建两个窗口或创建一个带有按钮的窗口的示例。 You can try what fits better for your game and rules.
您可以尝试更适合您的游戏和规则的方法。
from tkinter import *
def openNewWindow():
newWindow = Toplevel(root)
newWindow.title("New Window")
newWindow.geometry("300x300")
newWindowLabel = Label(newWindow, text='openNewWindow', font=('Helvetica', 16, 'bold')).place(x=50,y=50)
def openAnotherWindow():
otherWindow = Toplevel(root)
otherWindow.title("New Window")
otherWindow.geometry("300x300")
anotherWindowLabel = Label(otherWindow, text='openanotherWindow', font=('Helvetica', 16, 'bold')).place(x=50,y=50)
root = Tk()
root.title("Example tkinter windows")
root.geometry("300x350")
openNewWindow()
btn1 = Button(root,text="openNewWindow",command=openNewWindow)
btn1.place(x=100,y=100)
btn2 = Button(root,text="anotherWindow",command=openAnotherWindow)
btn2.place(x=100,y=200)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.