# -*- coding: utf-8 -*-
"""
Created on Wed Jun 6 18:03:38 2023
@author: SavingKing
"""
import tkinter as tk
root=tk.Tk()
root.title("listbox by SavingKing.com.tw")
root.geometry("350x300")
listbox=tk.Listbox(root)
listbox.insert(1,"SavingKing")
listbox.insert(2,"賴:@wvr5039s")
listbox.insert(3,"儲蓄保險王")
listbox.pack()
root.mainloop()GUI:

使用tk.END

使用StringVar 與 listvariable
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 6 18:03:38 2023
@author: SavingKing
"""
from typing import Dict,Tuple
cable:Dict[str,Tuple[float]] = {
"UFA125A": (13.95, 0.4),
"UED240E": (8.09, 0.76)}
import tkinter as tk
root=tk.Tk()
root.title("listbox by SavingKing.com.tw")
root.geometry("350x300")
menu = tk.StringVar(root, tuple(cable.items()) )
"""#不要漏掉root參數
tuple(cable.keys())
Out[61]: ('UFA125A', 'UED240E')
tuple(cable.items())
Out[67]: (('UFA125A', (13.95, 0.4)), ('UED240E', (8.09, 0.76)))
"""
listbox=tk.Listbox(root,listvariable=menu)
listbox.pack()
root.mainloop()GUI:

listbox.curselection()
#取得listbox 項目 含索引值的tuple #(1,)
listbox.get(n) #獲得該索引值的內容
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 6 18:03:38 2023
@author: SavingKing
"""
from typing import Dict,Tuple
cable:Dict[str,Tuple[float]] = {
"UFA125A": (13.95, 0.4),
"UED240E": (8.09, 0.76)}
import tkinter as tk
root=tk.Tk()
root.title("listbox by SavingKing.com.tw")
root.geometry("350x300")
# 定義 Label 顯示選取內容的函式
def show():
n, = listbox.curselection()
# 取得項目索引值,因為是單選,回傳 (i,),所以使用 n, 取值
text.set(listbox.get(n))
# 設定文字變數內容為該索引值對應的內容
text = tk.StringVar() # 設定文字變數
#默認第一個參數為root
label = tk.Label(root, textvariable=text) # 放入 Label
label.pack()
menu = tk.StringVar(root, tuple(cable.items()) )
"""#因為有第二個參數value=tuple,不可省略第一個root參數,
不然會把tuple當成root,觸發AttributeError:
'tuple' object has no attribute '_root'
tuple(cable.keys())
Out[61]: ('UFA125A', 'UED240E')
tuple(cable.items())
Out[67]: (('UFA125A', (13.95, 0.4)),
('UED240E', (8.09, 0.76)))
tk.Entry(root,textvariable=tk.StringVar(value="2"))
"""
listbox=tk.Listbox(root,listvariable=menu)
listbox.pack()
btn = tk.Button(root, text='顯示', command=show)
# 放入 Button,設定 command 參數
btn.pack()
root.mainloop()tk.StringVar?

menu = tk.StringVar(root, tuple(cable.items()) )
“””#因為有第二個參數value=tuple,
不可省略第一個root參數,
不然會把tuple當成root,觸發AttributeError:
‘tuple’ object has no attribute ‘_root’ “””
GUI:

listbox GUI

推薦hahow線上學習python: https://igrape.net/30afN

![Python-docx 圖片提取完全指南:從 rId 到二進位資料的探險rid ; part = doc.part.rels[rid].target_part #return part.blob if “ImagePart” in type(part).__name__ else None Python-docx 圖片提取完全指南:從 rId 到二進位資料的探險rid ; part = doc.part.rels[rid].target_part #return part.blob if “ImagePart” in type(part).__name__ else None](https://i1.wp.com/savingking.com.tw/wp-content/uploads/2026/01/20260113135812_0_8fa645.png?quality=90&zoom=2&ssl=1&resize=350%2C233)



![Python TQC考題810 最大值與最小值之差,L=[eval(i) for i in s.split()] Python TQC考題810 最大值與最小值之差,L=[eval(i) for i in s.split()]](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2022/05/20220507094526_36.png?quality=90&zoom=2&ssl=1&resize=350%2C233)



近期留言