Application Windows

Base Windows

In the simple examples we’ve used this far, there’s only one window on the screen; the root window. This is automatically created when you call the Tk constructor, and is of course very convenient for simple applications:

from Tkinter import *

root = Tk()

create window contents as children to root…

root.mainloop()

If you need to create additional windows, you can use the Toplevel widget. It simply creates a new window on the screen, a window that looks and behaves pretty much like the original root window:

from Tkinter import *

root = Tk()

create root window contents…

top = Toplevel()

create top window contents…

root.mainloop()

There’s no need to use pack to display the Toplevel, since it is automatically displayed by the window manager (in fact, you’ll get an error message if you try to use pack or any other geometry manager with a Toplevel widget).

Menus

Tkinter provides a special widget type for menus. To create a menu, you create an instance of the Menu class, and use add methods to add entries to it:

add_command(label=string, command=callback) adds an ordinary menu entry.

add_separator() adds an separator line. This is used to group menu entries.

add_cascade(label=string, menu=menu instance) adds a submenu (another Menu instance). This is either a pull-down menu or a fold-out menu, depending on the parent.

Here’s an example:

Creating a small menu

from Tkinter import *

def callback():

print “called the callback!”

root = Tk()

create a menu

menu = Menu(root)

root.config(menu=menu)

filemenu = Menu(menu)

menu.add_cascade(label=”File”, menu=filemenu)

filemenu.add_command(label=”New”, command=callback)

filemenu.add_command(label=”Open…”, command=callback)

filemenu.add_separator()

filemenu.add_command(label=”Exit”, command=callback)

helpmenu = Menu(menu)

menu.add_cascade(label=”Help”, menu=helpmenu)

helpmenu.add_command(label=”About…”, command=callback)

mainloop()

In this example, we start out by creating a Menu instance, and we then use the config method to attach it to the root window. The contents of that menu will be used to create a menubar at the top of the root window. You don’t have to pack the menu, since it is automatically displayed by Tkinter.

Next, we create a new Menu instance, using the menubar as the widget parent, and the add_cascade method to make it a pulldown menu. We then call add_command to add commands to the menu (note that all commands in this example use the same callback), and add_separator to add a line between the file commands and the exit command.

Finally, we create a small help menu in the same fashion.

Toolbars

Many applications place a toolbar just under the menubar, which typically contains a number of buttons for common functions like open file, print, undo, etc.

In the following example, we use a Frame widget as the toolbar, and pack a number of ordinary buttons into it.

Creating a simple toolbar

from Tkinter import *

root = Tk()

def callback():

print “called the callback!”

create a toolbar

toolbar = Frame(root)

b = Button(toolbar, text=”new”, width=6, command=callback)

b.pack(side=LEFT, padx=2, pady=2)

b = Button(toolbar, text=”open”, width=6, command=callback)

b.pack(side=LEFT, padx=2, pady=2)

toolbar.pack(side=TOP, fill=X)

mainloop()

The buttons are packed against the left side, and the toolbar itself is packed against the topmost side, with the fill option set to X. As a result, the widget is resized if necssary, to cover the full with of the parent widget.

Also note that I’ve used text labels rather than icons, to keep things simple. To display an icon, you can use the PhotoImage constructor to load a small image from disk, and use the image option to display it.

Status Bars

Finally, most applications sport a status bar at the bottom of each application window. Implementing a status bar with Tkinter is trivial: you can simply use a suitably configured Label widget, and reconfigure the text option now and then. Here’s one way to do it:

status = Label(master, text=””, bd=1, relief=SUNKEN, anchor=W)

status.pack(side=BOTTOM, fill=X)

If you wish to be fancy, you can use the following class instead. It wraps a label widget in a convenience class, and provides set and clear methods to modify the contents.

A Status Bar Class (File: tkSimpleStatusBar.py)

class StatusBar(Frame):

def __init__(self, master):

Frame.__init__(self, master)

self.label = Label(self, bd=1, relief=SUNKEN, anchor=W)

self.label.pack(fill=X)

def set(self, format, *args):

self.label.config(text=format % args)

self.label.update_idletasks()

def clear(self):

self.label.config(text="")

self.label.update_idletasks()

The set method works like C’s printf function; it takes a format string, possibly followed by a set of arguments (a drawback is that if you wish to print an arbitrary string, you must do that as set(“%s”, string)). Also note that this method calls the update_idletasks method, to make sure pending draw operations (like the status bar update) are carried out immediately.

But the real trick here is that we’ve inherited from the Frame widget. At the cost of a somewhat awkward call to the frame widget’s constructor, we’ve created a new kind of custom widget that can be treated as any other widget. You can create and display the status bar using the usual widget syntax:

status = StatusBar(root)

status.pack(side=BOTTOM, fill=X)

We could have inherited from the Label widget itself, and just extended it with set and clear methods. This approach have a few drawbacks, though:

It makes it harder to maintain the status bar’s integrity. Some team members may cheat, and use config instead of set. That’s not a big deal, until the day you decide to do some extra processing in the set method. Or the day you decide to use a Canvas widget to implement a fancier status bar.

It increases the risk that your additional methods conflict with attributes or methods used by Tkinter. While the Frame and Toplevel widgets have relatively few methods, other widgets can have several dozens of widget specific attributes and methods.

Future versions of Tkinter may use factory functions rather than class constructors for most widgets. However, it’s more or less guaranteed that such versions will still provide Frame and Toplevel classes. Better safe than sorry, in other words.

python画星空的程序_Python Tkinter 应用程序窗口相关推荐

  1. 用python画烟花简单画法_python实现烟花小程序

    本文实例为大家分享了python实现烟花小程序的具体代码,供大家参考,具体内容如下 ''' FIREWORKS SIMULATION WITH TKINTER *self-containing cod ...

  2. python frame如何置顶_Python tkinter frame父窗口小部件排列列

    我把滚动条放在一个框架里,框架放在一个小部件里.这个框架上面有一个标签.上面的标签有三列.带有滚动条的框架有三列.我无法让框架内和框架上方的三根柱子对齐.在 如果您能帮我排好纵队,我们将不胜感激.谢谢 ...

  3. python画星空的程序_用python画星空源代码是什么?

    用python画星空源代码是什么? 用python画星空源代码是from turtle import * from random import random,randint screen = Scre ...

  4. python绘制星空_用python画星空源代码是什么?

    用python画星空源代码是什么? 用python画星空源代码是from turtle import * from random import random,randint screen = Scre ...

  5. python星空代码_用python画星空源代码是什么?

    用python画星空源代码是什么? 用python画星空源代码是from turtle import * from random import random,randint screen = Scre ...

  6. 用python画星空-用python画星空源代码是什么?

    7mr少儿编程网-Scratch_Python_教程_免费儿童编程学习平台 用python画星空源代码是什么?7mr少儿编程网-Scratch_Python_教程_免费儿童编程学习平台 用python ...

  7. 用python画星空的代码_用python画星空源代码是什么?_后端开发

    用python画星空源代码是什么? 用python画星空源代码是 from turtle import * from random import random,randint screen = Scr ...

  8. python源代码制作星空_用python画星空源代码是什么?

    用python画星空源代码是什么? 用python画星空源代码是 from turtle import *from random import random,randintscreen = Scree ...

  9. 用python画星空-【Python】手把手教你绘制星空旅游线路图

    0 前言 5月是美国的毕业季,我家领导也即将毕业了,我们将在美国进行为期两周的毕业旅行.其实类似这样的航线图我以前也用PPT绘制过,给领导用于发朋友圈,虽然总体能表达出意思,但是效果不能让自己满意,见 ...

最新文章

  1. 打开和关闭Oracle Flashback
  2. 安安猜价格聪明机器人_5 项降噪优化,石头扫地机器人 T6 安静也有大吸力
  3. php html区别_php与html区别
  4. matlab guide对话框+滑动条+弹出式菜单+列表框的使用
  5. php面向对象有哪三种访问控制模式,第三节 访问控制的实现
  6. 寻剑气世界java,Java——Unsafe
  7. jQ实现简单放大镜效果
  8. 【虚拟机VMWare15使用】给虚拟机硬盘添加还原卡//使用物理磁盘做虚拟硬盘
  9. 干货 | 扫了那么多二维码,15款工具打造你的专属二维码
  10. adb shell 小米手机_Ubuntu下adb连接小米手机
  11. 佰维存储通过注册:拟募资8亿 国家集成电路基金是二股东
  12. 使用igvtools可视化测序深度分布
  13. 智能电动车无感解锁方案:设备篇
  14. 要点初见:从旅行青蛙开始的OpenCV3模板匹配功能探索
  15. 一入职就遇上Mysql亿级优化,方案都改了5遍
  16. tkinter 出现两个窗口 tk(未响应) 解决方法
  17. LAMP和LNMP架构(介绍)
  18. es模糊查询与精准查询混用、距离排序、返回距离
  19. qt中label画圆代表指示灯_使用qpaint和paintEvent在PYQT5中QLabel中包含的Pixmap上绘制圆...
  20. 文本安装everest linux

热门文章

  1. 【AIOT】1.1 物联网导学
  2. 浅谈QPS、TPS、并发用户数、吞吐量的关系
  3. Win10内存占用过高 并且与任务管理器不符
  4. 微信公众号用户认证机制升级方案
  5. Go语言开源12年---Go Team感言
  6. 奈学教育架构师训练营
  7. python 画曼陀罗花_巧用Adobe Illustrator绘制精美的曼陀罗花
  8. FFplay文档解读-21-音频过滤器六
  9. LiteFlow v2.7.1版本发布新版官网上线
  10. python元组添加元素_Python 元组_python元组操作_添加元素_python 元组转字符串