Tk Tutorial - 9. Windows and Dialogs
来源:互联网 发布:阿里云华北5 编辑:程序博客网 时间:2024/06/06 17:10
Creating and Destroying Windows
Toplevel windows are created using the Toplevel function:
t = Toplevel(parent)Unlike regular widgets, you don't have to "grid" a toplevel for it to appear onscreen. Once you've created a new toplevel,you can then create other widgets which are children of that toplevel, and grid them inside the toplevel. In other words,the new toplevel behaves exactly like the automatically created root window.
To destroy a window, you can use the destroy method on a widget:
window.destroy()Note that you can use destroy on any widget, not just a toplevel window.
Changing Window Behavior and Styles
Window Title
oldtitle = window.title()window.title('New title')
Size and Location
A full geometry specification looks like this:
widthxheight±x±yWidth and height (normally in pixels) are pretty self-explanatory. The "x" (horizontal position) is specified with a leading plus or minus, so"+25" means the left edge of the window should be 25 pixels from the left edge of the screen, while"-50" means the right edge of the window should be 50 pixels from the right edge of the screen. Similarly, a "y" (vertical) position of "+10" means the top edge of the window should be ten pixels below the top of the screen, while "-100" means the bottom edge of the window should be 100 pixels above the bottom of the screen.
window.geometry('300x200-5+40')
Stacking Order
Stacking order refers to the order that windows are "placed" on the screen, from bottom to top.
You can obtain the current stacking order, a list from lowest to highest, via:
root.tk.eval('wm stackorder '+str(window))You can also just check if one window is above or below another:
if (root.tk.eval('wm stackorder '+str(window)+' isabove '+str(otherwindow))=='1') ...if (root.tk.eval('wm stackorder '+str(window)+' isbelow '+str(otherwindow))=='1') ...You can also raise or lower windows, either to the very top (bottom) of the stacking order, or just above (below) a designated window:
window.lift()window.lift(otherwin)window.lower()window.lower(otherwin)So if you have several widgets gridded together but overlapping, you can raise and lower them relative to each other. For example:
from tkinter import *from tkinter import ttkroot = Tk()little = ttk.Label(root, text="Little")bigger = ttk.Label(root, text='Much bigger label')little.grid(column=0,row=0)bigger.grid(column=0,row=0)root.after(2000, lambda: little.lift())root.mainloop()
Resizing Behavior
You can prevent it from being resized, in fact independently specifying whether the window's width (first parameter) can be changed, as well as its height (second parameter). So to disable all resizing:
window.resizable(FALSE,FALSE)Remember that if you've added a ttk::sizegrip widget to the window, that you should remove it if you're making the window non-resizable.
If resizing is enabled, you can specify a minimum and/or maximum size that you'd like the window's size to be constrained to (again, parameters are width and height):
window.minsize(200,100)window.maxsize(500,500)
Iconifying and Withdrawing
The possible states for a window include "normal" and "iconic" (for an iconified window), as well as several others:"withdrawn","icon" or "zoomed".
You can query or set the current window state, and there are also the methods "iconify" and"deiconify" which are shortcuts for setting the "iconic" or"normal" states respectively.
thestate = window.state()window.state('normal')window.iconify()window.deiconify()
Standard Dialogs
Selecting Files and Directories
On Windows and Mac,these invoke the underlying operating system dialogs directly.
from tkinter import filedialogfilename = filedialog.askopenfilename()filename = filedialog.asksaveasfilename()dirname = filedialog.askdirectory()All of these commands produce modal dialogs. The commands return the full pathname of the file or directory the user has chosen, or return an empty string if the user cancels out of the dialog.
Selecting Colors
There is also a modal dialog to let the user select a color. It will return a color value, e.g. "#ff62b8".The dialog takes an optional"initialcolor" option to specify an existing color that the user is presumably replacing.
from tkinter import colorchoosercolorchooser.askcolor(initialcolor='#ff0000')
Alert and Confirmation Dialogs
Tk provides a versatile "message box" that encapsulates all these different types of dialogs.
from tkinter import messageboxmessagebox.showinfo(message='Have a good day')
messagebox.askyesno( message='Are you sure you want to install SuperVirus?' icon='question' title='Install')Like the previous dialogs that we've seen, these are modal, and will return the result of the user's action to the caller. The exact return value will depend on the"type" option passed to the command, as shown here:
- Tk Tutorial - 9. Windows and Dialogs
- Tk Tutorial - 15. Styles and Themes
- Tk Tutorial - 4. Tk Concepts
- iPhone and iPad JavaScript dialogs (alert, confirm, prompt) and windows
- Tk Tutorial - 8. Menus
- Tk Tutorial - 12. Canvas
- Tk Tutorial - 13. Text
- Tk Tutorial - 14. Tree
- SIP transactions and dialogs
- Windows 7 Task Dialogs
- Tk Tutorial - 5. Basic Widgets
- Tk Tutorial - 7. More Widgets
- Shellcoding for Linux and Windows Tutorial
- Shellcoding for Linux and Windows Tutorial
- Shellcoding for Linux and Windows Tutorial
- tcl/tk and python
- Tk Tutorial - 3. A First (Real) Example
- Tk Tutorial - 6. The Grid Geometry Manager
- Android使用ListView应该注意的地方
- java学习之路
- gdb远程调试
- android修改字体大小
- 染色体畸变对身体有什么作用?
- Tk Tutorial - 9. Windows and Dialogs
- 什么是染色体畸变?
- 求最长递减子序列(转载)
- 给PHP安装和开启curl扩展
- Android4.4实现PPPOE拨号上网具体流程
- 汇总程序员年度十大懊恼事件
- hdu 4883 TIANKENG’s restaurant(区间选点)
- Java 包
- Unable to execute dex: Multiple dex files define Lcom/anysdk/framework/java/R$drawable;