Search code examples
pythonpython-3.xtkinterpycharmmessagebox

Using "messagebox.showinfo" in PyCharm


I'm trying to use messagebox.showinfo to show a message when my counter reach 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()

The first button "btnBalle" call the function "Balle()".

It works when I run it in Python IDLE but when I use PyCharm, it doesn't.


Solution

  • Obviously that's not your whole program, but… let's assume your real program is calling Test() multiple times, but isn't actually starting the Tk runloop anywhere, or even creating a top-level Tk object. So, any Tkinter windows you try to display will never show up. (Actually, on some platforms, like OS X, it will show up—but it shouldn't, and if it doesn't on your platform, that's not a bug.)

    Why does it work in IDLE? Because IDLE itself is written in Tkinter, and the tricks it uses to allow you to use the interactive interpreter while you've got a GUI program running also allow you to get away with this. That's not something you should rely on.