python 如何删除frame 中的 label标签控件_玩转图形界面编程—解析Python小挑战No12(2)...
《Python真好玩,教孩子學編程》小挑戰【轉載】
第十二章,玩轉圖形界面編程書籍《Python真好玩,教孩子學編程》
講解了如何通過tkinter模塊來完成圖形交互界面
tkinter小挑戰(二)
(1)使用grid布局管理方式布局一個計算器。
from tkinter import Tk from tkinter import Frame from tkinter import Label from tkinter import Buttonroot = Tk() root.geometry('310x400') root.title("計算器")#定義最上面的空白面板 frame_show = Frame(width=300,height = 5) frame_show.pack()#定義顯示字符串的標簽,anchor設置控件的錨點(東南西北) show_label = Label(frame_show, text = "0",width=300, height=3,font=("黑體", 16, "bold"),anchor='e') show_label.pack(padx=10, pady=1)#定義按鈕區 frame_button = Frame(width=300,height = 180) frame_button.pack(padx=5, pady=1) #第一行 b_1 = Button(frame_button, text="MC", width=6, height=3).grid(row=0, column=0) b_2 = Button(frame_button, text="MR", width=6, height=3).grid(row=0, column=1) b_3 = Button(frame_button, text="MS", width=6, height=3).grid(row=0, column=2) b_4 = Button(frame_button, text="M+", width=6, height=3).grid(row=0, column=3) b_5 = Button(frame_button, text="M-", width=6, height=3).grid(row=0, column=4) #第二行 b_6 = Button(frame_button, text="←", width=6, height=3).grid(row=1, column=0) b_7 = Button(frame_button, text="CE", width=6, height=3).grid(row=1, column=1) b_8 = Button(frame_button, text="C", width=6, height=3).grid(row=1, column=2) b_9 = Button(frame_button, text="±", width=6, height=3).grid(row=1, column=3) b_10 = Button(frame_button, text="√", width=6, height=3).grid(row=1, column=4)#第三行 b_11 = Button(frame_button, text="7", width=6, height=3).grid(row=2, column=0) b_12 = Button(frame_button, text="8", width=6, height=3).grid(row=2, column=1) b_13 = Button(frame_button, text="9", width=6, height=3).grid(row=2, column=2) b_14 = Button(frame_button, text="/", width=6, height=3).grid(row=2, column=3) b_15 = Button(frame_button, text="%", width=6, height=3).grid(row=2, column=4) #第4行 b_16 = Button(frame_button, text="4", width=6, height=3).grid(row=3, column=0) b_17 = Button(frame_button, text="5", width=6, height=3).grid(row=3, column=1) b_18 = Button(frame_button, text="6", width=6, height=3).grid(row=3, column=2) b_19 = Button(frame_button, text="*", width=6, height=3).grid(row=3, column=3) b_20 = Button(frame_button, text="1/x", width=6, height=3).grid(row=3, column=4) #第5行 b_21 = Button(frame_button, text="1", width=6, height=3).grid(row=4, column=0) b_22 = Button(frame_button, text="2", width=6, height=3).grid(row=4, column=1) b_23 = Button(frame_button, text="3", width=6, height=3).grid(row=4, column=2) b_24 = Button(frame_button, text="-", width=6, height=3).grid(row=4, column=3) b_25 = Button(frame_button, text="=", width=6, height=6).grid(row=4, column=4,rowspan=2) #第6行 b_26 = Button(frame_button, text="0", width=13, height=3).grid(row=5, column=0,columnspan = 2) b_27 = Button(frame_button, text=".", width=6, height=3).grid(row=5, column=2) b_28 = Button(frame_button, text="+", width=6, height=3).grid(row=5, column=3)root.mainloop()運行程序,結果為:
windows系統的效果
mac系統的效果
解析:
程序中,使用了pack和grid兩種布局管理方式,
pack:指定相對位置的布局方式,不要求精準布局的時候,可以選擇使用。
grid:按照行和列的方式來排列組件。組件的位置由行號和列號指定。
使用了3種控件:
1. Label組件:展示文字
2. Button組件:按鈕組件
3. Frame組件:這是一個框架組件,用來在屏幕上顯示一個矩形區域,多作為容器來布局窗體。
程序中,把計算器劃分了兩個矩形區域,分別是:
frame_show:頭部的空白區域。
frame_button:按鈕區域。
接下來看下具體的代碼:
from tkinter import Tk from tkinter import Frame from tkinter import Label from tkinter import Button導入tkinter模塊中需要使用的函數和組件
root = Tk()創建根窗口
root.geometry('310x400')設置窗口大小為:310x400,這里的x就是小寫英文字母x
root.title("計算器")設置窗口標題為"計算器"
#定義最上面的空白面板 frame_show = Frame(width=300,height = 5) frame_show.pack()通過Frame組件劃分了最上面的空白面板,設置width=300,height = 5
#定義顯示字符串的標簽,anchor設置控件的錨點(東南西北) show_label = Label(frame_show, text = "0",width=300, height=3,font=("黑體", 16, "bold"),anchor='e') show_label.pack(padx=10, pady=1)創建了標簽組件,在窗口中展示。設置的屬性有:
text:設置標簽組件展示的文字是0。
width:設置標簽組件的長為300。
height:設置標簽組件的寬為3。
font :設置文字的字體為黑體,大小為16并且加粗。
anchor:設置文字的位置在標簽的東的方向。
padx:設置標簽組件的水平方向的外邊距。
pady:設置標簽組件的垂直方向的外邊距。
#定義按鈕區 frame_button = Frame(width=300,height = 180) frame_button.pack(padx=5, pady=1)通過Frame組件劃分了按鈕區域,設置width=300,height = 180
#第一行 b_1 = Button(frame_button, text="MC", width=6, height=3).grid(row=0, column=0) b_2 = Button(frame_button, text="MR", width=6, height=3).grid(row=0, column=1) b_3 = Button(frame_button, text="MS", width=6, height=3).grid(row=0, column=2) b_4 = Button(frame_button, text="M+", width=6, height=3).grid(row=0, column=3) b_5 = Button(frame_button, text="M-", width=6, height=3).grid(row=0, column=4) #第二行 b_6 = Button(frame_button, text="←", width=6, height=3).grid(row=1, column=0) b_7 = Button(frame_button, text="CE", width=6, height=3).grid(row=1, column=1) b_8 = Button(frame_button, text="C", width=6, height=3).grid(row=1, column=2) b_9 = Button(frame_button, text="±", width=6, height=3).grid(row=1, column=3) b_10 = Button(frame_button, text="√", width=6, height=3).grid(row=1, column=4)#第三行 b_11 = Button(frame_button, text="7", width=6, height=3).grid(row=2, column=0) b_12 = Button(frame_button, text="8", width=6, height=3).grid(row=2, column=1) b_13 = Button(frame_button, text="9", width=6, height=3).grid(row=2, column=2) b_14 = Button(frame_button, text="/", width=6, height=3).grid(row=2, column=3) b_15 = Button(frame_button, text="%", width=6, height=3).grid(row=2, column=4) #第4行 b_16 = Button(frame_button, text="4", width=6, height=3).grid(row=3, column=0) b_17 = Button(frame_button, text="5", width=6, height=3).grid(row=3, column=1) b_18 = Button(frame_button, text="6", width=6, height=3).grid(row=3, column=2) b_19 = Button(frame_button, text="*", width=6, height=3).grid(row=3, column=3) b_20 = Button(frame_button, text="1/x", width=6, height=3).grid(row=3, column=4) #第5行 b_21 = Button(frame_button, text="1", width=6, height=3).grid(row=4, column=0) b_22 = Button(frame_button, text="2", width=6, height=3).grid(row=4, column=1) b_23 = Button(frame_button, text="3", width=6, height=3).grid(row=4, column=2) b_24 = Button(frame_button, text="-", width=6, height=3).grid(row=4, column=3) b_25 = Button(frame_button, text="=", width=6, height=6).grid(row=4, column=4,rowspan=2) #第6行 b_26 = Button(frame_button, text="0", width=13, height=3).grid(row=5, column=0,columnspan = 2) b_27 = Button(frame_button, text=".", width=6, height=3).grid(row=5, column=2) b_28 = Button(frame_button, text="+", width=6, height=3).grid(row=5, column=3)通過grid布局管理方式布局計算器的各種按鈕。
使用到的屬性有:
row:行號
column:列號
rowspan:行跨度,多用來合并一列中的多個單元。
columnspan:列跨度,多用來合并一行中的多個單元。
root.mainloop()讓根窗口持續展示。
意外收獲:
使用mac系統和windows運行同一段代碼的時候,發現一樣的代碼,效果居然不一樣。
在mac系統上運行正常,但是windows系統中會很丑。
所以當在windows運行的時候,
把b_25的height改成5,其他所有Button組件的height改成2,會獲得更好的效果,文章開頭的效果圖就是調整之后的效果。
代碼為:
from tkinter import Tk from tkinter import Frame from tkinter import StringVar from tkinter import Label from tkinter import Buttonroot = Tk() root.geometry('310x400') root.title("計算器")#定義最上面的空白面板 frame_show = Frame(width=300,height=10) frame_show.pack()#定義顯示字符串的標簽,anchor設置控件的錨點(東南西北) show_label = Label(frame_show, text = "0",width=300, height=3,font=("黑體", 16, "bold"),anchor='e') show_label.pack(padx=10, pady=1)#定義按鈕區 frame_button = Frame(width=300,height=180) frame_button.pack(padx=5, pady=1) #第一行 b_1 = Button(frame_button, text="MC", width=6, height=2).grid(row=0, column=0) b_2 = Button(frame_button, text="MR", width=6, height=2).grid(row=0, column=1) b_3 = Button(frame_button, text="MS", width=6, height=2).grid(row=0, column=2) b_4 = Button(frame_button, text="M+", width=6, height=2).grid(row=0, column=3) b_5 = Button(frame_button, text="M-", width=6, height=2).grid(row=0, column=4) #第二行 b_6 = Button(frame_button, text="←", width=6, height=2).grid(row=1, column=0) b_7 = Button(frame_button, text="CE", width=6, height=2).grid(row=1, column=1) b_8 = Button(frame_button, text="C", width=6, height=2).grid(row=1, column=2) b_9 = Button(frame_button, text="±", width=6, height=2).grid(row=1, column=3) b_10 = Button(frame_button, text="√", width=6, height=2).grid(row=1, column=4)#第三行 b_11 = Button(frame_button, text="7", width=6, height=2).grid(row=2, column=0) b_12 = Button(frame_button, text="8", width=6, height=2).grid(row=2, column=1) b_13 = Button(frame_button, text="9", width=6, height=2).grid(row=2, column=2) b_14 = Button(frame_button, text="/", width=6, height=2).grid(row=2, column=3) b_15 = Button(frame_button, text="%", width=6, height=2).grid(row=2, column=4) #第4行 b_16 = Button(frame_button, text="4", width=6, height=2).grid(row=3, column=0) b_17 = Button(frame_button, text="5", width=6, height=2).grid(row=3, column=1) b_18 = Button(frame_button, text="6", width=6, height=2).grid(row=3, column=2) b_19 = Button(frame_button, text="*", width=6, height=2).grid(row=3, column=3) b_20 = Button(frame_button, text="1/x", width=6, height=2).grid(row=3, column=4) #第5行 b_21 = Button(frame_button, text="1", width=6, height=2).grid(row=4, column=0) b_22 = Button(frame_button, text="2", width=6, height=2).grid(row=4, column=1) b_23 = Button(frame_button, text="3", width=6, height=2).grid(row=4, column=2) b_24 = Button(frame_button, text="-", width=6, height=2).grid(row=4, column=3) b_25 = Button(frame_button, text="=", width=6, height=5).grid(row=4, column=4,rowspan=2) #第6行 b_26 = Button(frame_button, text="0", width=13, height=2).grid(row=5, column=0,columnspan = 2) b_27 = Button(frame_button, text=".", width=6, height=2).grid(row=5, column=2) b_28 = Button(frame_button, text="+", width=6, height=2).grid(row=5, column=3)root.mainloop()您的鼓勵是我最大的動力
總結
以上是生活随笔為你收集整理的python 如何删除frame 中的 label标签控件_玩转图形界面编程—解析Python小挑战No12(2)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 3 字典排序_Python
- 下一篇: eclipse的jsp第一行代码报错_机