tkinter:打开一个带有按钮提示的新窗口 [英] tkinter: Open a new window with a button prompt

查看:79
本文介绍了tkinter:打开一个带有按钮提示的新窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何能够通过用户在 tkinter GUI 中按下按钮来打开一个新窗口?我只需要非常简单的解决方案,如果代码也能解释清楚就太好了.

How would I be able to open a new window by the user pressing a button in a tkinter GUI? I only need quite simple solutions, and if the code could be explained as well that would be great.

推荐答案

这是针对您的问题的几乎最短的解决方案.该解决方案适用于 python 3.x.对于 python 2.x,将 import 更改为 Tkinter 而不是 tkinter(区别在于大写):

Here's the nearly shortest possible solution to your question. The solution works in python 3.x. For python 2.x change the import to Tkinter rather than tkinter (the difference being the capitalization):

import tkinter as tk
#import Tkinter as tk  # for python 2
    
def create_window():
    window = tk.Toplevel(root)

root = tk.Tk()
b = tk.Button(root, text="Create new window", command=create_window)
b.pack()

root.mainloop()

这绝对不是我推荐的良好编码风格示例,但它说明了基本概念:带有命令的按钮和创建窗口的函数.

This is definitely not what I recommend as an example of good coding style, but it illustrates the basic concepts: a button with a command, and a function that creates a window.

这篇关于tkinter:打开一个带有按钮提示的新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆