tkinter禁用(只讀)下拉列表Combobox問題
tkinter禁用(只讀)下拉列表Combobox
tkinter將下拉列表框Combobox控件的狀態(tài)設置為只讀,也就是不可編輯狀態(tài):
# 定義下拉列表值 self.Combo3List = [1, 2, 3,] # 定義下拉列表控件,self.Frame1為Combobox的父級, 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) # 設置Combobox的默認值為Combo3List集合的第一個元素 self.Combo3.set(self.Combo3List[0])
tkinter中大部分控件都擁有state屬性,上面代碼段中的state屬性也可以在Combobox的Configure()函數(shù)中進行配置。
如下:
# 設置狀態(tài)為只讀 self.Combo3.configure(state="readonly") # 設置狀態(tài)為禁用 #self.Combo3.configure(state="disabled")
tkinter知識點使用記錄
引入模塊與創(chuàng)建實例
import tkinter as tk root = tk.TK()
窗口屬性設置
# 設置窗口標題 root.title('考試廣播系統(tǒng)') # 設置窗口圖標 root.iconbitmap(EXAM_ICON) # 設置窗口背景色 root.configure(background='#d4d0c8') # 禁用調整GUI大小 root.resizable(0, 0) # 獲取屏幕寬高 sc_w = self.winfo_screenwidth() sc_h = self.winfo_screenheight() # 設置窗口大小 root.geometry(f"560x360+{(sc_w - 560) // 2}+{(sc_h - 360) // 2 - 40}") # 隱藏窗口,設置后窗口固定 root.overrideredirect(1)
退出時彈窗確定
from tkinter import messagebox def quit_ui(): ?? ?if messagebox.askokcancel("退出", "你想退出窗口嗎?"): ?? ? ? self.quit() ?? ? ? self.destroy() # 設置窗口屬性時一并設置 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ù),可以使用下面這種方法:
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)
鼠標放在按鈕提示信息
import Pmw balloon = Pmw.Balloon(root) # 創(chuàng)建按鈕對象 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)建控件后,如何先布局再賦值,那么控件對象是無效,需要先賦值再布局
# 這種方式是無法實現(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對象,那么tk.StringVar()需要在__init__方法中聲明,不然不能使用
fwq_var_name = tk.StringVar() # 輸入框設置初始值 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('圖片所在絕對路徑') 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)
動態(tài)更改控件的屬性
# 比如動態(tài)更改按鈕顯示的圖片,在config中修改指定參數(shù)即可 show_btn.config(image=take_right_img)
實現(xiàn)下拉菜單
km_var_name = tk.StringVar() ?# 接收下拉選擇的值 SUBJECT_LIST = ("語文", "數(shù)學", "英語", "物理", "化學", "地理", "歷史") 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)
效果參考:
實現(xiàn)切換導航欄
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="語音播放內容", alue=1, anchor="n", indicatoron=0, command=self.show_or_hide_info).place(x=488, y=336)
效果參考:
實現(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')}] 啟動服務") info_list_box.insert('end', f"[{time.strftime('%Y-%m-%d %H:%M:%S')}] 更新語音文件成功") frame.place(width=480, height=124, x=380, y=362)
效果參考:
解決獲取不到新窗口文本框內容
使用了tk.Tk()
方法來新建窗口,這樣得到的是一個新的根窗口,無法與原來的根窗口進行有效交互。
因此需要使用Toplevel
組件新建頂級窗口
new_tk = tk.Toplevel()
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python Sweetviz輕松實現(xiàn)探索性數(shù)據(jù)分析
Sweetviz是一個開放源代碼Python庫,可生成精美的高密度可視化文件,以單行代碼啟動EDA(探索性數(shù)據(jù)分析)。輸出是一個完全獨立的HTML應用程序,該系統(tǒng)圍繞快速可視化目標值和比較數(shù)據(jù)集而構建。其目標是幫助快速分析目標特征,訓練與測試數(shù)據(jù)以及其他此類數(shù)據(jù)表征任務2021-11-11python DataFrame獲取行數(shù)、列數(shù)、索引及第幾行第幾列的值方法
下面小編就為大家分享一篇python DataFrame獲取行數(shù)、列數(shù)、索引及第幾行第幾列的值方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04Python數(shù)據(jù)分析應用之Matplotlib數(shù)據(jù)可視化詳情
這篇文章主要介紹了Python數(shù)據(jù)分析應用之Matplotlib數(shù)據(jù)可視化詳情,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-06-06