Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python
Python
  1. ให้ Focus กับ File Dialog ใน Tkinter

    File dialog (filedialog.askopenfilename) จะได้ focus อัตโนมัติหาก parent window ถูกสร้างก่อนและเป็นหน้าต่างหลัก ตัวอย่างโค้ด from tkinter import * from tkinter import filedialog from PIL import Image, ImageTk win = Tk() win.geometry(700x350) win.title(File Explorer) def open_file(): win.filen

  2. ฟังก์ชัน wait_window() ใน Tkinter ทำงานอย่างไร

    wait_window(window) หยุดการทำงานของหน้าต่างหลัก (main window) จนกว่าหน้าต่างที่ระบุ (มักเป็น Toplevel) จะถูกทำลาย (destroy) เหมาะสำหรับ dialog box ที่ต้องรอผู้ใช้ตอบสนอง ตัวอย่างโค้ด from tkinter import * win = Tk() win.geometry(700x350) top = Toplevel(win) top.geometry(450x150) Label(top, text=T

  3. Widget สำหรับตารางแบบ Excel ใน Tkinter คือ Treeview

    Tkinter Treeview widget (ในโมดูล ttk) เป็น widget หลักสำหรับสร้างตารางข้อมูลคล้าย Excel รองรับ: หลายคอลัมน์ พร้อมหัวคอลัมน์ แทรก/ลบ/แก้ไขแถวข้อมูล การเลือกหลายแถว (multi-select) เชื่อมต่อกับ Pandas/NumPy สำหรับประมวลผลข้อมูล ตัวอย่างโค้ด from tkinter import * from tkinter import ttk win = Tk() w

  4. เรียกใช้งานปุ่ม (Button) โดยไม่ต้องคลิกใน Tkinter

    เมธอด invoke() ของ Button widget ใช้เรียก callback function โดยไม่ต้องมีการคลิกจริง เหมาะสำหรับการทริกเกอร์เหตุการณ์อัตโนมัติ ตัวอย่างโค้ด from tkinter import * from tkinter import ttk from tkinter import messagebox win = Tk() win.geometry(700x350) def display_msg(): messagebox.showinfo(Messa

  5. เลือกหลายรายการใน Treeview ด้วยเมาส์ (Multi-select)

    Treeview รองรับการเลือกหลายรายการ (multi-select) โดยค้างปุ่ม Ctrl แล้วคลิกรายการที่ต้องการ ไม่ต้องเขียนโค้ดเพิ่มเติม เพียงแค่ใช้ widget แบบปกติ ตัวอย่างโค้ด from tkinter import * from tkinter import ttk win = Tk() win.geometry(700x350) s = ttk.Style() s.theme_use(clam) tree = ttk.Treeview(win, c

  6. ทำให้สี่เหลี่ยมผืนผ้าใน Tkinter Canvas โปร่งใส (Transparent)

    Canvas ไม่รองรับ alpha โดยตรง ต้องสร้างภาพ RGBA ด้วย Pillow คล้ายวิธีการกับรูปทรงทั่วไป ตัวอย่างโค้ด from tkinter import * from PIL import Image, ImageTk win = Tk() win.geometry(700x350) images = [] def create_rectangle(x, y, a, b, **options): if alpha in options: alpha = int(options

  7. เปลี่ยนสีพื้นหลังเมนูของ OptionMenu Widget ใน Tkinter

    OptionMenu เป็น dropdown list สำหรับเลือกตัวเลือก สามารถปรับสีพื้นหลังของเมนูย่อยได้ผ่าน option[menu].config(bg=color) ตัวอย่างโค้ด from tkinter import * from PIL import Image, ImageTk win = Tk() win.geometry(700x350) Label(win, text=Select a Day from the Menu, font=(Arial 13)).pack(pady=10) var

  8. สร้างตารางข้อมูล (Table-like) ด้วย Tkinter Treeview

    Treeview widget ใน ttk ใช้สร้างตารางข้อมูลคล้าย Excel รองรับคอลัมน์หลายคอลัมน์ และการแทรกข้อมูลแบบไดนามิก ตัวอย่างโค้ด from tkinter import * from tkinter import ttk win = Tk() win.geometry(700x350) s = ttk.Style() s.theme_use(clam) tree = ttk.Treeview(win, column=(c1, c2, c3), show=headings, hei

  9. จัดการเหตุการณ์คลิกปุ่ม (Button Click Event) ใน Tkinter

    Button widget ใช้รับเหตุการณ์คลิกจากผู้ใช้ โดยระบุ command parameter ให้เป็นฟังก์ชัน callback สามารถใช้ lambda สำหรับฟังก์ชันแบบไม่มีชื่อ (anonymous function) ตัวอย่างโค้ด from tkinter import * from tkinter import messagebox from tkinter import ttk win = Tk() win.geometry(700x350) def show_msg():

  10. ปรับค่า Alpha (ความโปร่งใส) ของรูปทรงใน Tkinter Canvas

    Canvas widget ใน Tkinter ไม่รองรับ alpha channel โดยตรง ต้องสร้างภาพ RGBA ด้วย Pillow แล้ววาดบน Canvas หลักการ สร้างฟังก์ชัน create_rectangle รับพารามิเตอร์ alpha คำนวณค่า alpha (0-1) เป็น 0-255 สร้างภาพ RGBA ด้วย Image.new(RGBA, size, color+alpha) วาดภาพบน Canvas ด้วย create_image ตัวอย่างโค้ด

  11. แสดงกล้องเว็บแคม (Webcam) ในหน้าต่าง Tkinter ด้วย OpenCV

    รวม OpenCV (Computer Vision) กับ Tkinter เพื่อแสดงภาพจากกล้องเว็บแคมแบบเรียลไทม์ ต้องติดตั้ง: pip install opencv-python pip install Pillow หลักการทำงาน OpenCV จับภาพจากกล้อง (cv2.VideoCapture(0)) แปลงภาพ BGR → RGB สร้าง PIL Image จาก array แปลงเป็น ImageTk.PhotoImage สำหรับ Tkinter แสดงใน Label w

  12. แสดง LaTeX แบบเรียลไทม์ในกล่องข้อความ Tkinter

    การแสดงสูตรคณิตศาสตร์ LaTeX ใน Tkinter ต้องใช้ Matplotlib พร้อม backend TkAgg วาดสูตรบน Figure แล้วแสดงใน Canvas ขั้นตอนหลัก Import: matplotlib, tkinter, ttk, TkAgg backend สร้าง Frame, Entry widget สำหรับรับ LaTeX syntax สร้าง Figure และ Canvas ผ่าน FigureCanvasTkAgg เขียนฟังก์ชันอ่านค่า Entry แป

  13. แก้ไขสไตล์หัวตาราง (Heading) ใน Treeview ด้วย ttk.Style

    Treeview widget ใช้สร้างตารางข้อมูล การปรับสไตล์หัวคอลัมน์ (heading) ใช้ ttk.Style กับ selector Treeview.Heading ตัวอย่างโค้ด from tkinter import * from tkinter import ttk win = Tk() win.geometry(700x350) s = ttk.Style() s.theme_use(clam) # ปรับสีพื้นหลังหัวตาราง s.configure(Treeview.Heading, ba

  14. วิธีทับซ้อน Widget/Frame ใน Tkinter ด้วย Place Geometry Manager

    Geometry Manager ใน Tkinter มี 3 แบบ: pack, grid, place สำหรับการทับซ้อน widget ให้ใช้ place() ซึ่งจัดตำแหน่งด้วยพิกัด x, y แบบสัมพัทธ์หรือสัมบูรณ์ การกำหนดพิกัด x, y เดียวกันให้หลาย widget จะทำให้ทับซ้อนกัน ตัวอย่างโค้ด from tkinter import * from tkinter import ttk win = Tk() win.geometry(700x35

  15. เปลี่ยนสีหัวแท็บ (Tab Header) ใน ttk.Notebook

    การปรับสีแท็บใน Notebook widget ต้องใช้ ttk.Style และกำหนดสไตล์สำหรับ TNotebook.Tab ร่วมกับธีม default ตัวอย่างโค้ด from tkinter import * from tkinter import ttk win = Tk() win.geometry(700x350) s = ttk.Style() s.theme_use(default) s.configure(TNotebook.Tab, background=green3) s.map(TNotebook, ba

  16. วิธีผูกปุ่ม Shift+Tab ใน Tkinter

    Tkinter รองรับการผูกคีย์คอมโบ (key combination) เช่น <Shift-Tab>, <Control-c> เป็นต้น ผ่านเมธอด bind() ตัวอย่างโค้ด from tkinter import * from tkinter import messagebox win = Tk() win.geometry(700x350) def show_msg(e): messagebox.showinfo(Message, Hey There! I hope you are doing w

  17. เปิด URL ในเว็บเบราว์เซอร์ด้วยโมดูล webbrowser ใน Python

    โมดูล webbrowser ใน Python ให้ฟังก์ชันสำหรับเปิด URL ในเบราว์เซอร์เริ่มต้นของระบบ ไม่ต้องติดตั้งเพิ่มเติม (built-in module) การใช้งานพื้นฐาน import webbrowser url = https://www.tutorialspoint.com/ webbrowser.open_new_tab(url) # เปิดในแท็บใหม่ # webbrowser.open(url) # เปิดในหน้าต่างใหม่ ต

  18. วิธีเปลี่ยนธีม (Theme) ทั้งแอปพลิเคชัน Tkinter

    โมดูล ttk (Themed Tkinter) ให้การจัดการธีมและสไตล์ widget แบบสมัยใหม่ ธีมที่รองรับใน Tkinter มีดังนี้: winnative (Windows native) clam alt default classic vista xpnative ใช้เมธอด style.theme_use(theme_name) เพื่อเปลี่ยนธีมทั้งแอปพลิเคชัน ตัวอย่างโค้ด from tkinter import * from tkinter import ttk

  19. วิธีแสดงและซ่อน Widget ใน Tkinter

    ใน Tkinter สามารถควบคุมการแสดง/ซ่อน widget ได้ง่ายด้วย Geometry Manager: แสดง widget: ใช้ pack(), grid() หรือ place() ซ่อน widget: ใช้ pack_forget(), grid_forget() หรือ place_forget() ตัวอย่างโค้ด (ใช้ pack) from tkinter import * from tkinter import ttk win = Tk() win.geometry(700x350) style =

  20. วิธีล้างรายการทั้งหมดใน ttk.Treeview Widget

    Treeview widget ใช้แสดงข้อมูลในรูปตาราง หากต้องการลบข้อมูลทั้งหมด ให้ใช้เมธอด get_children() เพื่อดึงรายการทุกอย่าง แล้ววนลูปลบด้วย delete(item) ตัวอย่างโค้ด from tkinter import * from tkinter import ttk win = Tk() win.geometry(700x350) s = ttk.Style() s.theme_use(clam) def clear_all(): for

Total 9001 -คอมพิวเตอร์  FirstPage PreviousPage NextPage LastPage CurrentPage:299/451  20-คอมพิวเตอร์/Page Goto:1 293 294 295 296 297 298 299 300 301 302 303 304 305