在PyCharm中使用"messagebox.showinfo

-1 投票
2 回答
2143 浏览
提问于 2025-05-01 09:19

我想用messagebox.showinfo来显示一个消息,当我的计数器到达4的时候。

import tkinter
from tkinter import *

global cptBalle
global cptPrise
cptBalle = 0
cptPrise = 0
cptRetrait = 0
cptManche = 1
ptsVisiteur = 0
ptsReceveur = 0
coupSurVisiteur = 0
coupSurReceveur = 0
erreurVisiteur = 0
erreurReceveur = 0
equipeAuBaton = "visiteur"



def balle():
    global cptBalle
    global cptPrise

    cptBalle += 1

    if cptBalle == 4:
        messagebox.showinfo(title="Balle", message="Testtest")
        cptBalle = 0
        cptPrise = 0



def prise():
    pass

def fausse_balle():
    pass

def retrait():
    pass

def balle_passee():
    pass

def mauvais_lancer():
    pass

def sacrifice():
    pass

def simple():
    pass

def double():
    pass

def triple():
    pass

def circuit():
    pass

def atteint():
    pass

def erreur():
    pass

def creer_bouton():
    btnBalle = Button(app, text="Balle", command=balle)
    btnBalle.grid(row=1, column=0)

    btnPrise = Button(app, text="Prise", command=prise)
    btnPrise.grid(row=2, column=0)

    btnFausse_balle = Button(app, text="Fausse balle", command=fausse_balle)
    btnFausse_balle.grid(row=3, column=0)

    btnRetrait = Button(app, text="Retrait", command=retrait)
    btnRetrait.grid(row=4, column=0)

    btnBalle_passee = Button(app, text="Balle passee", command=balle_passee)
    btnBalle_passee.grid(row=5, column=0)

    btnMauvais_lancer = Button(app, text="Mauvais lancer", command=mauvais_lancer)
    btnMauvais_lancer.grid(row=6, column=0)

    btnSacrifice = Button(app, text="Sacrifice", command=sacrifice)
    btnSacrifice.grid(row=7, column=0)

    btnSimple = Button(app, text="Simple", command=simple)
    btnSimple.grid(row=8, column=0)

    btnDouble = Button(app, text="Double", command=double)
    btnDouble.grid(row=9, column=0)

    btnTriple = Button(app, text="Triple", command=triple)
    btnTriple.grid(row=10, column=0)

    btnCircuit = Button(app, text="Circuit", command=circuit)
    btnCircuit.grid(row=11, column=0)

    btnAtteint = Button(app, text="Atteint", command=atteint)
    btnAtteint.grid(row=12, column=0)

    btnErreur = Button(app, text="Erreur", command=erreur)
    btnErreur.grid(row=13, column=0)



root = tkinter.Tk()
root.title("Baseball!")
root.geometry("750x350")

app = Frame(root)
app.grid()

creer_bouton()


root.mainloop()

第一个按钮“btnBalle”会调用一个叫“Balle()”的函数。

在Python IDLE里运行的时候是可以的,但在PyCharm里就不行。

暂无标签

2 个回答

0

我在使用消息框类的时候也遇到了同样的问题。

我代码的基础是:

import tkinter as tk

tk.messagebox.showinfo( --- )

我通过添加以下内容解决了这个问题:

from tkinter import messagebox

然后我调用了:

messagebox.showinfo( --- )

希望这能帮到你。

2

显然,这不是你整个程序的代码,但我们假设你的真实程序多次调用了Test(),但实际上并没有在任何地方启动Tk的运行循环,也没有创建一个顶层的Tk对象。所以,你尝试显示的任何Tkinter窗口都不会出现。(实际上,在某些平台上,比如OS X,它会显示出来——但这并不是正常情况,如果在你的平台上没有显示,那也不是bug。)

为什么在IDLE中可以正常工作?因为IDLE本身就是用Tkinter写的,它使用的一些技巧让你在运行图形界面程序时也能使用交互式解释器,这样就可以做到这一点。但这并不是你应该依赖的方式。

撰写回答