Python tkinter例項 簡單介紹

2021-06-21 11:44:07 字數 1462 閱讀 8538

#!/usr/bin/env  python3.3

#coding=utf-8

import tkinter as tk

from tkinter import ttk

def sample1_hello_title():

'''1, 最簡單'''

root = tk.tk()

root.title('你好, 這是tkinter世界!')

root.mainloop()

def sample2_hello_label():

'''2, 新增乙個標籤'''

root = tk.tk()

root.title('示例')

label = ttk.label(root, text='你好,歡迎來到tkinter世界!')

label.pack()

root.mainloop()

def sample3_hello2():

def __init__(self, master=none):

self.master = master

frame = ttk.frame(master)

frame.pack(expand="yes", fill="both")

#輸入框

self.msgvar = tk.stringvar()

self.msgvar.set('歡迎來到tkinter的世界')

self.input = ttk.entry(frame, textvariable=self.msgvar)

self.input.pack(fill='x', padx=10, pady=10)

#提示資訊框

self.caption = ttk.label(frame,text='')

self.caption.pack(expand='yes')

#命令面板

commandpane = ttk.frame(frame)

commandpane.pack(pady=10)

self.btnhello = ttk.button(commandpane, text='您好', command=self.say_hi)

self.btnhello.pack(side='left')

self.button = ttk.button(commandpane, text='退出', command=frame.quit)

self.button.pack()

def say_hi(self):

self.caption['text']= '你好,%s!' % self.input.get()

if __name__ == '__main__':

#sample1_hello_title()

#sample2_hello_label()

sample3_hello2()

Python tkinter三種布局例項詳解

一 pack布局舉例 pack布局案例 import tkinter basefram程式設計客棧e tkinter.tk 以下 都是建立乙個元件,然後布局 btn1 tkinter.button baseframe,text a btn1.pack side tkinter.left,expand...

Python Tkinter 布局方式

優點問題,如何在介面上顯示 定義 所有的tkinter 元件都包含專用的幾何管理方法,這些方法是用來組織和管理整個父配件區中子配件的布局的。tkinter 提供了截然不同的三種幾何管理類 pack grid 和place。pack 幾何管理採用塊的方式組織配件,在快速生成介面設計中廣泛採用,若干元件...

python tkinter 單選 多選

單選按鈕 tkinter.radiobutton root,text a pack tkinter.radiobutton root,text b pack tkinter.radiobutton root,text c pack 預設被選中,且單個一組 variable把radiobutton分成...