tab

需要注意的地方

Pack to make visible 如下方法使tab顯示

tabControl.pack(expand=1, fill="both")

# -*- coding: utf-8 -*-

# import
import tkinter as tk  # 1 imports
from tkinter import ttk

from tkinter import scrolledtext as st
from tkinter import Menu

win = tk.Tk()  # 2 Create instance
win.title("Python GUI")  # 3 Add a title
# win.resizable(0, 0)           # 4 Disable resizing the GUI

menuBar = Menu(win)
win.config(menu=menuBar)

# 下劃線這種命名方式表明這是私有函數(shù)不是被客戶端調(diào)用的
def _quit():
    win.quit()
    win.destroy()
    exit()

fileMenu = Menu(menuBar)
fileMenu.add_command(label="New")
# 分隔線
fileMenu.add_separator()
fileMenu.add_command(label="Exit", command=_quit)
menuBar.add_cascade(label="File", menu=fileMenu)

helpMenu = Menu(menuBar)
helpMenu.add_command(label="About")
menuBar.add_cascade(label="Help", menu=helpMenu)

# create tab control
tabControl = ttk.Notebook(win)
# create a tab
tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)
# add a tab
tabControl.add(tab1, text='我是tab1')
tabControl.add(tab2, text='我是tab2')
# Pack to make visible
tabControl.pack(expand=1, fill="both")

# We are creating a container frame to hold all other widgets
monty = ttk.LabelFrame(tab1, text=' Monty Python')
# monty = ttk.LabelFrame(win, )
monty.grid(column=0, row=0, padx=8, pady=4)

# 為tab2添加容器
monty2 = ttk.LabelFrame(tab2, text=' The Snake ')
monty2.grid(column=0, row=0, padx=8, pady=4)


# add a label                   #4
aLabel = ttk.Label(monty, text="輸入文本:").grid(column=0, row=0, sticky=tk.W)  # 5

ttk.Label(monty, text="choose a number").grid(column=1, row=0, sticky=tk.W)
number = tk.StringVar()

# only be able to select the values we have programmed into the Combobox:state="readonly"
numberChosen = ttk.Combobox(monty, width=12, textvariable=number, state="readonly")
numberChosen.grid(column=1, row=1, sticky=tk.W)
numberChosen["values"] = (1, 2, 3, 4, 5, 6, 12)
numberChosen.current(3)


def clickMe():
    action.configure(text="hello " + name.get() + "-" + number.get())
    # aLabel.configure(foreground="red")


# add a button                   #4
action = ttk.Button(monty, text="點(diǎn)我", command=clickMe)
action.grid(column=2, row=1)
# action.configure(state="disabled")  # Disable the Button Widget

# Adding a Textbox Entry widget    # 5
name = tk.StringVar()
nameEntered = ttk.Entry(monty, width=12, textvariable=name)
nameEntered.grid(column=0, row=1, sticky=tk.W)
nameEntered.focus()  # Place cursor into name Entry

# Creating three checkbuttons    # 1
# 0 (unchecked) or 1 (checked) so the type of the variable is a tkinter integer.
chVarDis = tk.IntVar()  # 2
check1 = tk.Checkbutton(monty2, text="Disabled", variable=chVarDis, state='disabled')  # 3
check1.select()  # 4
check1.grid(column=0, row=4, sticky=tk.W)  # 5

chVarUn = tk.IntVar()  # 6
check2 = tk.Checkbutton(monty2, text="UnChecked", variable=chVarUn)
check2.deselect()  # 8
check2.grid(column=1, row=4, sticky=tk.W)  # 9

chVarEn = tk.IntVar()  # 10
check3 = tk.Checkbutton(monty2, text="Enabled", variable=chVarEn)
check3.select()  # 12
check3.grid(column=2, row=4, sticky=tk.W)  # 13

tk.Scrollbar()


# 代碼重構(gòu)(refactor our code)
# First, we change our Radiobutton global variables into a list.
colors = ["Blue", "Gold", "Red"]
# create three Radiobuttons using one variable
radVar = tk.IntVar()
print(radVar)
# Next we are selecting a non-existing index value for radVar.
# (如果不設(shè)置為range范圍外的值,初始化頁面默認(rèn)會選中第一個(gè)并且不會觸發(fā)變更背景色的回調(diào)函數(shù))
radVar.set(99)
# We have also changed the callback function to be zero-based, using the list instead of module-level global variables.
# Radiobutton callback function
def radCall():
    radSel = radVar.get()
    # if radSel == 0:
    #     win.configure(background=colors[0])
    # elif radSel == 1:
    #     win.configure(background=colors[1])
    # elif radSel == 2:
    #     win.configure(background=colors[2])

    if radSel == 0:
        monty2.configure(text='Blue')
    elif radSel == 1:
        monty2.configure(text='Gold')
    elif radSel == 2:
        monty2.configure(text='Red')
# Now we are creating all three Radiobutton widgets within one loop.
for col in range(3):
    curRad = 'rad' + str(col)
    curRad = tk.Radiobutton(monty2, text=colors[col], variable=radVar, value=col, command=radCall)
    curRad.grid(column=col, row=5, sticky=tk.W)

# Using a scrolled Text control
scrollW = 30
scrollH = 3
scroll = st.ScrolledText(monty2, width=scrollW, height=scrollH, wrap=tk.WORD)
scroll.grid(column=0, columnspan=3, sticky='WE') # sticky='WE' 該屬性左右對其,做下面的測試時(shí)可以注釋查看效果
# scroll.grid(column=0, columnspan=3)


# Create a container to hold labels(label的長度取決于LabelFrame標(biāo)題的長度,當(dāng)添加的LabelFrame組件的長度大于硬編碼的組件大小時(shí),
# 我們會自動將這些組件移動到column 0 列的中心,并在組件左右兩側(cè)填充空白,具體可以參看下列兩行的區(qū)別)
labelsFrame = ttk.LabelFrame(monty2, text=' Labels in a Frame ')
# labelsFrame = ttk.LabelFrame(win)
# labelsFrame.grid(column=0, row=7, padx=20, pady=40)
labelsFrame.grid(column=0, row=7)

# Place labels into the container element # 2
ttk.Label(labelsFrame, text='Label 1').grid(column=0, row=0)
ttk.Label(labelsFrame, text='Label 2').grid(column=0, row=1)
ttk.Label(labelsFrame, text='Label 3').grid(column=0, row=2)
# Place cursor into name Entry
nameEntered.focus()

win.mainloop()  # 5 Start GUI
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容