2017-01-30 4 views
1

Ich habe ein Problem beim Erstellen eines Popup-Fensters für ein Programm.Wie erstelle ich ein Popup-Fenster in tkinter?

Code:

from tkinter import * 
from tkinter import ttk 
import tkinter as tk 

def popupBonus(): 
    popupBonusWindow = tk.Tk() 
    popupBonusWindow.wm_title("Window") 
    labelBonus = Label(popupBonusWindow, text="Input") 
    labelBonus.grid(row=0, column=0) 
    B1 = ttk.Button(popupBonusWindow, text="Okay", command=popupBonusWindow.destroy()) 
    B1.pack() 

class Application(ttk.Frame): 
    def __init__(self, master): 
     ttk.Frame.__init__(self, master) 
     mainwindow = ttk.Frame(self) 

     self.buttonBonus = ttk.Button(self, text="Bonuses", command=popupBonus) 
     self.buttonBonus.pack() 

Der Code erzeugt ein Fenster mit einem Button und wenn Sie die Taste drücken, es sollte ein Popup-Fenster mit dem Titel „Fenstern“, Text „Input“ erzeugen, und eine Schaltfläche "Okay" sagen, um das Popup-Fenster zu verlassen und zum Hauptfenster zurückzukehren. Allerdings bekomme ich diesen Fehler.

Traceback (most recent call last): 
    File "D:\Softwares\Python 3.6.0\lib\tkinter\__init__.py", line 1699, in __call__ 
return self.func(*args) 
    File "C:\Users\J---- M--\Desktop\Python\GUI-Messagebox 5.py", line 12, in popupBonus 
B1 = ttk.Button(popupBonusWindow, text="Okay", command=popupBonusWindow.destroy()) 
    File "D:\Softwares\Python 3.6.0\lib\tkinter\ttk.py", line 614, in __init__ 
Widget.__init__(self, master, "ttk::button", kw) 
    File "D:\Softwares\Python 3.6.0\lib\tkinter\ttk.py", line 559, in __init__ 
tkinter.Widget.__init__(self, master, widgetname, kw=kw) 
    File "D:\Softwares\Python 3.6.0\lib\tkinter\__init__.py", line 2293, in __init__ 
(widgetName, self._w) + extra + self._options(cnf)) 
_tkinter.TclError: NULL main window 

Ich habe keine Ahnung, was das Problem ist. Ich habe versucht, für 4 Stunden eine Antwort zu finden und habe im Grunde aufgegeben.

Auch ich möchte nicht tkinter die Messagebox-Funktion verwenden, weil ich nicht das Ausrufezeichen Bild will und ich mehrere Kontrollkästchen in das Popup-Fenster später einschließen möchten.

+1

a) nicht mehr als eine Instanz von 'Tk' erstellen, und b) finden Sie unter http: // effbot.org/tkinterbook/tkinter-dialog-windows.htm –

+0

'command =' erwartet Funktionsname (Callback) - es bedeutet ohne '()'. Nun führst du 'popupBonusWindow.destroy()' und sein Ergebnis - wahrscheinlich 'None' - zu' command = ' – furas

Antwort

5

fand ich 3 Fehler

  • Verwendung Toplevel() statt Tk() zum zweiten/dritten Fenster
  • command= erwartet Rückruf erstellen - Funktionsnamen ohne ()
    (aber Sie verwenden popupBonusWindow.destroy())
  • nicht Mischen pack() und grid() in einem Fenster oder Rahmen
    (aber Sie verwenden grid() und pack() in Pop-up)

Sie können aber auch wie showinfo() Einbau-Message verwenden

import tkinter as tk 
from tkinter import ttk 
from tkinter.messagebox import showinfo 

def popup_bonus(): 
    win = tk.Toplevel() 
    win.wm_title("Window") 

    l = tk.Label(win, text="Input") 
    l.grid(row=0, column=0) 

    b = ttk.Button(win, text="Okay", command=win.destroy) 
    b.grid(row=1, column=0) 

def popup_showinfo(): 
    showinfo("Window", "Hello World!") 

class Application(ttk.Frame): 

    def __init__(self, master): 
     ttk.Frame.__init__(self, master) 
     self.pack() 

     self.button_bonus = ttk.Button(self, text="Bonuses", command=popup_bonus) 
     self.button_bonus.pack() 

     self.button_showinfo = ttk.Button(self, text="Show Info", command=popup_showinfo) 
     self.button_showinfo.pack() 

root = tk.Tk() 

app = Application(root) 

root.mainloop()