tkinter禁用(只讀)下拉列表Combobox問(wèn)題
tkinter禁用(只讀)下拉列表Combobox
tkinter將下拉列表框Combobox控件的狀態(tài)設(shè)置為只讀,也就是不可編輯狀態(tài):
# 定義下拉列表值
self.Combo3List = [1, 2, 3,]
# 定義下拉列表控件,self.Frame1為Combobox的父級(jí), state表示Combobox的狀態(tài),readonly為只讀,disabled為禁用
self.Combo3 = Combobox(self.Frame1, values=self.Combo3List, font=("宋體",9), state="readonly")
# 放置控件
self.Combo3.place(relx=0.884, rely=0.627, relwidth=0.053, relheight=0.089)
# 設(shè)置Combobox的默認(rèn)值為Combo3List集合的第一個(gè)元素
self.Combo3.set(self.Combo3List[0])tkinter中大部分控件都擁有state屬性,上面代碼段中的state屬性也可以在Combobox的Configure()函數(shù)中進(jìn)行配置。
如下:
# 設(shè)置狀態(tài)為只讀 self.Combo3.configure(state="readonly") # 設(shè)置狀態(tài)為禁用 #self.Combo3.configure(state="disabled")
tkinter知識(shí)點(diǎn)使用記錄
引入模塊與創(chuàng)建實(shí)例
import tkinter as tk root = tk.TK()
窗口屬性設(shè)置
# 設(shè)置窗口標(biāo)題
root.title('考試廣播系統(tǒng)')
# 設(shè)置窗口圖標(biāo)
root.iconbitmap(EXAM_ICON)
# 設(shè)置窗口背景色
root.configure(background='#d4d0c8')
# 禁用調(diào)整GUI大小
root.resizable(0, 0)
# 獲取屏幕寬高
sc_w = self.winfo_screenwidth()
sc_h = self.winfo_screenheight()
# 設(shè)置窗口大小
root.geometry(f"560x360+{(sc_w - 560) // 2}+{(sc_h - 360) // 2 - 40}")
# 隱藏窗口,設(shè)置后窗口固定
root.overrideredirect(1)退出時(shí)彈窗確定
from tkinter import messagebox
def quit_ui():
?? ?if messagebox.askokcancel("退出", "你想退出窗口嗎?"):
?? ? ? self.quit()
?? ? ? self.destroy()
# 設(shè)置窗口屬性時(shí)一并設(shè)置
root.protocol('WM_DELETE_WINDOW', quit_ui)如何向綁定方法中傳遞參數(shù)
一般向按鈕添加事件方法:
tk.Button(self, text='登錄(L)', width=10, bg="#d4d0c8", command=login_btn).place(x=164, y=300)
如果向在綁定方法時(shí)傳遞參數(shù),可以使用下面這種方法:
tk.Button(self, text='登錄(L)', width=10, bg="#d4d0c8", command=lambda: login_btn('l')).place(x=164, y=300)按鈕綁定快捷鍵
# 在方法中定義觸發(fā)事件
root.bind_all('<Control-l>', login_btn)鼠標(biāo)放在按鈕提示信息
import Pmw
balloon = Pmw.Balloon(root)
# 創(chuàng)建按鈕對(duì)象
quit_btn = tk.Button(self, image=take_quit_img, bg="#d4d0c8",
? ? ? ? ? ? ??? ??? ? command=lambda: _audio_control("quit"))
quit_btn.place(width=30, height=130, x=870, y=382)
balloon.bind(quit_btn, "隱藏控件")注意:在創(chuàng)建控件后,如何先布局再賦值,那么控件對(duì)象是無(wú)效,需要先賦值再布局
# 這種方式是無(wú)法實(shí)現(xiàn)賦值的
quit_btn = tk.Button(self, image=take_quit_img, bg="#d4d0c8",
? ? ? ? ? ? ??? ??? ? command=lambda: _audio_control("quit")).place(width=30, height=130, x=870, y=382)輸入框接收數(shù)據(jù)
注意:如果是在類中創(chuàng)建tk對(duì)象,那么tk.StringVar()需要在__init__方法中聲明,不然不能使用
fwq_var_name = tk.StringVar()
# 輸入框設(shè)置初始值
fwq_var_name.set("七星耀月")
tk.Entry(root, textvariable=fwq_var_name, width=38, bd=3).place(x=250, y=100)如何顯示圖片
# 比如為按鈕控件添加圖片
take_ws_img = tk.PhotoImage('圖片所在絕對(duì)路徑')
sshow_btn = tk.Button(root, image=take_ws_img , bg="#d4d0c8", command=show_other_btn)
show_btn.place(width=50, height=50, x=813, y=13)動(dòng)態(tài)更改控件的屬性
# 比如動(dòng)態(tài)更改按鈕顯示的圖片,在config中修改指定參數(shù)即可 show_btn.config(image=take_right_img)
實(shí)現(xiàn)下拉菜單
km_var_name = tk.StringVar() ?# 接收下拉選擇的值
SUBJECT_LIST = ("語(yǔ)文", "數(shù)學(xué)", "英語(yǔ)", "物理", "化學(xué)", "地理", "歷史")
sub_box = ttk.Combobox(root, textvariable=km_var_name)
sub_box["values"] = SUBJECT_LIST
sub_box.current(0)
sub_box.bind("<<ComboboxSelected>>", get_sub_box)
sub_box.place(width=150, height=24, x=175, y=103)效果參考:

實(shí)現(xiàn)切換導(dǎo)航欄
btn_choose_value = tk.IntVar() btn_choose_value.set(0) tk.Radiobutton(root, variable=self.btn_choose_value, bg="#d4d0c8", anchor="n", text="信息提示", value=0, indicatoron=0, command=self.show_or_hide_info).place(x=380, y=336) tk.Radiobutton(root, variable=self.btn_choose_value, bg="#d4d0c8", text="語(yǔ)音播放內(nèi)容", alue=1, anchor="n", indicatoron=0, command=self.show_or_hide_info).place(x=488, y=336)
效果參考:

實(shí)現(xiàn)列表
menu_frame = tk.Frame()
frame = tk.LabelFrame(root, labelwidget=menu_frame, bg="white", borderwidth=2, padx=10, pady=8, relief="sunken")
y_bar = tk.Scrollbar(frame, orient="vertical", bd=0, width=14)
list_box = tk.Listbox(frame, bg="white", yscrollcommand=y_bar.set, border=0, highlightthickness=0, selectforeground="blue", selectbackground="#d4d0c8", activestyle="none", font=("微軟雅黑", 8), height=4)
y_bar['command'] = list_box.yview
y_bar.pack(side="right", fill="y")
list_box.pack_forget()
info_list_box = tk.Listbox(frame, bg="white", yscrollcommand=y_bar.set, border=0, highlightthickness=0, font=("微軟雅黑", 8), height=4)
y_bar['command'] = info_list_box.yview
y_bar.pack(side="right", fill="y")
info_list_box.pack(anchor="nw", fill="both", expand="yes")
info_list_box.insert('end', f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] 啟動(dòng)服務(wù)")
info_list_box.insert('end', f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] 更新語(yǔ)音文件成功")
frame.place(width=480, height=124, x=380, y=362)
效果參考:

解決獲取不到新窗口文本框內(nèi)容
使用了tk.Tk()方法來(lái)新建窗口,這樣得到的是一個(gè)新的根窗口,無(wú)法與原來(lái)的根窗口進(jìn)行有效交互。
因此需要使用Toplevel組件新建頂級(jí)窗口
new_tk = tk.Toplevel()
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決Tensorboard可視化錯(cuò)誤:不顯示數(shù)據(jù) No scalar data was found
今天小編就為大家分享一篇解決Tensorboard可視化錯(cuò)誤:不顯示數(shù)據(jù) No scalar data was found,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
python2 與 pyhton3的輸入語(yǔ)句寫(xiě)法小結(jié)
這篇文章主要給大家介紹了關(guān)于python2 與 pyhton3的輸入語(yǔ)句寫(xiě)法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09
解決在Python編輯器pycharm中程序run正常debug錯(cuò)誤的問(wèn)題
今天小編就為大家分享一篇解決在Python編輯器pycharm中程序run正常debug錯(cuò)誤的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01
?python中pandas讀取csv文件?時(shí)如何省去csv.reader()操作指定列步驟
這篇文章主要介紹了?python中pandas讀取csv文件?時(shí)如何省去csv.reader()操作指定列步驟,對(duì)正在工作的你可能有一定的幫助,需要的朋友可以參考一下2022-01-01
使用XML庫(kù)的方式,實(shí)現(xiàn)RPC通信的方法(推薦)
下面小編就為大家?guī)?lái)一篇使用XML庫(kù)的方式,實(shí)現(xiàn)RPC通信的方法(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06

