tkinter:使用按钮提示打开新窗口 [英] tkinter: Open a new window with a button prompt

查看:272
本文介绍了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.

推荐答案

这是最短的您的问题的可能解决方案。该解决方案适用于python2.x。对于python 3.x,将导入更改为 tkinter而不是 Tkinter:

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

import Tkinter as tk

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天全站免登陆