發表文章

建立一個美化版的 console 視窗

#!/usr/bin/env python3 # -*- coding: utf-8 -*- import tkinter as tk from tkinter import font def output_to_console (text): global console console . insert(tk . END, text + " \n " ) console . see(tk . END) # 自動滾動到最底部 def clear_console (): global console console . delete( 1.0 , tk . END) # 清除所有文本 def create_window (): global console # 將 console 設置為全域變數以便外部函數訪問 # 創建主視窗 root = tk . Tk() root . title( "測試程式" ) # 設定視窗大小 window_width = 400 window_height = 400 # 獲取螢幕寬高 screen_width = root . winfo_screenwidth() screen_height = root . winfo_screenheight() # 計算視窗起始位置 position_top = int (screen_height / 2 - window_height / 2 ) position_right = int (screen_width / 2 - window_width / 2 ) # 設定視窗位置 root . geometry(f '{window_width}x{window_height}+{position_right}+{position_top}' ) # 定義按鈕點擊函數 def on_button_click (): label . c

global variable 奇怪的問題

從別的地方的呼叫 output_to_console 函數,裏面的 global console 竟然不是同一個,真是神奇了。 解決方法: python — 怎麼在兩個檔案之間分享全域變數 main.py import tkinter as tk import threading # 初始化全域變數 console = None console_initialized = threading . Event() # 用於等待 console 初始化完成 def set_console (widget): global console console = widget console_initialized . set() # 設置 console 已初始化完成 def output_to_console (text): global console print (f "output_to_console: id(console) = {id(console)}" ) # 打印 console 的 id 來檢查是否相同 if console is not None : console . insert(tk . END, text + " \n " ) console . see(tk . END) # 自動滾動到最底部 else : print ( "console 未初始化,無法輸出訊息" ) def create_window (): global console root = tk . Tk() root . title( "Console Example" ) set_console(tk . Text(root, wrap = tk . WORD, width = 50 , height = 20 , bg = "light yellow" , bd = 3 , relief = "solid" )) console .

建立一個類似 console 的視窗

這個程式碼是透過 Copilot 產生出來的,一開始都無法成功,中間還有時候給出上次完全一樣的錯誤程式碼,沒有仔細看還會被騙過,搞了快一小時總會產生可用的程式碼。 這個功能只是在 main.py 產生一個類似 console 的視窗,按了按鈕後執行其它 py 檔的函數,然後可以把資料輸出到 console 視窗。自己寫一直出現錯誤,也找不到為什麼,只好借助AI來幫忙。 main.py import tkinter as tk import config # 確保匯入 config 模組 def create_window (): root = tk . Tk() root . title( "Console Example" ) config . set_console(tk . Text(root, wrap = tk . WORD, width = 50 , height = 20 , bg = "light yellow" , bd = 3 , relief = "solid" )) config . console . pack(pady = 10 ) from external_script import external_function # 在 console 初始化完成後匯入 # 添加一個按鈕來呼叫 external_function test_button = tk . Button(root, text = "呼叫外部函數" , command = external_function) test_button . pack(pady = 10 ) root . after( 1000 , lambda : config . output_to_console( "這是一條測試訊息" )) root . mainloop() if __name__ == "__main__" : create_window() external_script.py from config im

Python List

create a=[] 新增一個空序列。 a=[1]*3=[1,1,1] 新增一個序列,裏面放了3個1。 a=[1 for i in range(3)]=[1,1,1] 新增一個序列,裏面放了3個1。 ';'.join(a) 將序列 a 產生一個字串 '1;1;1'。 Reference Python List必學實作 序列类型 --- list, tuple, range Python 推导式 P ython String join()

Selenium WebDriverWait

from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC def ready (browser:webdriver) -> bool : readyState = browser . execute_script( 'return document.readyState' ) if readyState == 'complete' : return True return False browser = webdriver . Firefox() browser . get( 'http://xxx.xxx.xxx' ) WebDriverWait(browser, 60 ) . until(ready) WebDriverWait(browser, 60 ) . until( lambda browser: browser . execute_script( 'return document.readyState' ) == 'complete' )

selenium timeout

timeout $ python3 Python 3.6.9 (default, Jan 26 2021, 15:33:00) [GCC 8.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from selenium import webdriver >>> browser = webdriver.Firefox() >>> browser.get('http://xxx.xxx.xxx') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/test/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 333, in get self.execute(Command.GET, {'url': url}) File "/home/test/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute self.error_handler.check_response(response) File "/home/test/.local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions

Selenium upload a file

filename = browser . find_element(By . NAME, "filename" ) filename . send_keys(os . path . abspath( "./test.bin" )) 使用 send_keys() 就可以輸入檔案,記住不可以輸入相對位置的檔案,會產生找不到檔案的錯誤。 os.path.abspath() 可以轉換相對位置的檔案。 Reference How to upload file with selenium (Python)?