importtkinterastkdefsay_hello():label.config(text="Hello, Tkinter!")# 创建主窗口root=tk.Tk()root.title("Tkinter Hello World")# 创建一个标签小部件label=tk.Label(root,text="Welcome to Tkinter!")# 将标签放入主窗口label.pack(pady=10)# 创建一个按钮小部件button=tk.Button(root,text="Say He...
Frame是一个可以装载其他控件的容器控件 如下所示的代码,表示创建一个根窗口,再在根窗口上创建一个Frame控件。 创建Frame时,设置了03节中提到的两个共用属性:relief,borderwidth, 第一个问题,如何知道它们取哪些值? 属性的取值一般位于constants模块中。 tk = Tk() frame = Frame(tk, relief=RIDGE, borderwidth...
frameConfig = Frame(self) frameConfig.grid(row=1, column=0) labelConfig = Label(frameConfig, text='网络配置', font=('Arial', 12), bg='green').grid(row=1, column=1, padx=2,pady=2, sticky='w') labelProtocol = Label(frameConfig, text='(1)协议类型', font=('Arial', 12), bg...
import tkinter as tk class Application: def __init__(self, master): frame = tk.Frame(master) frame.pack(side=tk.LEFT) # 默认是调整在最上面,可以自定义调整位置: LEFT, RIGHT, TOP, BOTTOM # frame.pack(side=tk.LEFT, padx=10, pady=10) # 距离窗口 self.label = tk.Label(frame, text=...
from tkinter import * #主窗口 win = Tk() win.title("C语言中文网") #创建一个frame窗体对象,用来包裹标签 frame = Frame (win, relief=SUNKEN, borderwidth=2, width=450, height=250) # 在水平、垂直方向上填充窗体 frame. pack (side=TOP, fill=BOTH, expand=1) # 创建 "位置1" Label1 = La...
window.geometry('500x150')## 创建第一个容器fm1 = tkinter.Frame()## 指定容器从左到右排列,沿水平和垂直方向填充,父容器增大时拉伸组件fm1.pack(side='left', fill='both',expand='YES')## 创建三个按钮,放置在fm1容器中,从上到下排列,水平填充,父容器增大时拉伸组件tkinter.Button(fm1, text='第...
importtkinterastk def say_hello(): name=entry.get()label.config(text=f"Hello, {name}!")# Create the main windowroot=tk.Tk()root.title("Tkinter Hello World")# Create a label widgetlabel=tk.Label(root,text="Welcome to Tkinter!")# Pack the label into the main windowlabel.pack(pady=...
To create a Tkinter Frame, you need to use theFrameclass provided by the Tkinter module. Here’s a basic example of how to create an empty frame with a light blue background. import tkinter as tk root = tk.Tk() frame = tk.Frame(root, bg="lightblue") ...
frame.pack() tk.mainloop() 键盘事件 from tkinter import * tk=Tk() Label(tk,text='按键事件触发').pack() def callback(event): print('EventType=',event.type) print('keysym=',event.keysym) frame=Frame(tk,bg='gold',width=100,height=100) frame.pack() tk.bind('<KeyPress-a>',call...
使用GUI面向对象编程写法,使用Application(Frame)来创建实例对象,创建组件的方法封装给CreateWidgetplace()方法,通过构造函数创建好组件 完整代码如下:fromtkinterimport*importrandomclassApplication(Frame):def__init__(self,master=None):#super()代表的是父类的定义,而不是父类对象super().__init__...