Loading AI tools
来自维基百科,自由的百科全书
Tkinter是Tk GUI工具包的Python绑定包。它是Tk GUI工具包的标准Python接口[3],并且是Python的业界标准GUI工具包。[4]Tkinter同时也包含在Python的Linux、Microsoft Windows和Mac OS X标准库中。Tkinter的名字来自Tk interface。
和大多数现代Tk绑定包一样,Tkinter是透过嵌入在Python解释器中的完整Tcl解释器来包装Python实现的。Tkinter的调用被翻译成Tcl命令,这些命令被提供给这个嵌入式解释器,从而使得在单个程序中可混合Python和Tcl。
它也有几种流行的GUI库可用作替代,例如wxPython、PyQt、PySide、Pygame、Pyglet和PyGTK。
该词在不同的文中具有不同的含义,通常指用户显示屏上的某处矩形区域。
指主窗口的子窗口。它将以桌面环境的标准框架和控件装饰。它可以在桌面上移动,通常也可以调整大小。
指图形用户界面中构成应用程序的任何构建块。
在Tkinter中,Frame(框架)小部件是复杂布局的基本组织单位。框架是一个矩形区域,可以包含其他小部件。
创建任何小部件时,都会创建父子关系。例如,如果将文本标签放置在框架内,则框架是标签的父小部件。
这是一个带有小部件的小型Python 3 Tkinter应用程序:[7]
#!/usr/bin/env python3
from tkinter import *
root = Tk() # 创建根窗口
w = Label(root, text="Hello, world!") # 创建一个带有单词的标签
w.pack() # 把标签放进窗口中
root.mainloop() # 开始主循环
对于 Python 2,唯一的区别是import命令中的“tkinter”是大写的“Tkinter (页面存档备份,存于互联网档案馆)”。[8]
创建小部件有四个步骤:[9]
在框架内创建一个小部件。
更改这个小部件的属性。
将它打包到指定的位置,使其可见。开发人员还可以选择使用 .grid()(row=整数,column=整数)(定义行和列来定位小部件,默认为 0)或.place()(relx=整数或小数,rely=整数或小数)(定义框架或窗口中的坐标)。
将其绑定到函数或事件。
这些步骤通常被简化,并且顺序可能会不同。
这是一个在 Python 中使用面向对象的简单的程序(使用Tcl 8.6,但MacOS上的Python默认不使用它):
#!/usr/bin/env python3
import tkinter as tk
class Application(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
self.mondialLabel = tk.Label(self, text='Hello World')
self.mondialLabel.config(bg="#00ffff")
self.mondialLabel.grid()
self.quitButton = tk.Button(self, text='Quit', command=self.quit)
self.quitButton.grid()
app = Application()
app.master.title('Sample application')
app.mainloop()
Seamless Wikipedia browsing. On steroids.
Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.
Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.