如何在 tkinter 中创建弹出窗口?

编程入门 行业动态 更新时间:2024-10-28 04:16:03
本文介绍了如何在 tkinter 中创建弹出窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在为程序创建弹出窗口时遇到问题.

I have a problem creating a popup window for a program.

代码:

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()

代码生成一个带有按钮的窗口,当你按下按钮时,它应该生成一个标题为Window"、文本为Input"的弹出窗口,并有一个按钮说好的"退出弹出窗口并返回到主窗口.但是,我收到此错误.

The code generates a window with a button and when you press the button, it's supposed to generate a popup window with title "Window", text "Input", and have a button saying "Okay" to exit popup window and return to main window. However, I am getting this error.

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

我不知道问题出在哪里.我尝试了 4 个小时寻找答案,但基本上放弃了.

I have no idea what the problem is. I have trying to find answer for 4 hours and basically gave up.

另外,我不想使用 tkinter 的消息框功能,因为我不想要感叹号图像,而且我想稍后在弹出窗口中包含多个复选框.

Also, I don't want to use tkinter's messagebox feature because I don't want the exclamation mark image and I want to include multiple checkboxs inside the popup window later on.

推荐答案

我发现了 3 个错误

  • 使用Toplevel()代替Tk()来创建第二/第三个窗口
  • command= 需要回调 - 没有 () 的函数名(但你使用 popupBonusWindow.destroy())
  • 不要在一个窗口或框架中混用 pack() 和 grid()(但您在弹出窗口中使用 grid() 和 pack() )
  • use Toplevel() instead of Tk() to create second/third window
  • command= expects callback - function name without () (but you use popupBonusWindow.destroy())
  • don't mix pack() and grid() in one window or frame (but you use grid() and pack() in popup)

但是你也可以使用内置的消息框,比如 showinfo()

But you can also use built-in messageboxes like showinfo()

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()

顺便说一句:我把它放在页面上:Tkinter:如何创建弹出窗口或消息框

更多推荐

如何在 tkinter 中创建弹出窗口?

本文发布于:2023-08-06 04:34:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1310102.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:弹出窗口   如何在   tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!