請寫一個python視窗程式 1. 大小800x600 2. 放上九個文字方塊,以3x3排列,寬度與高度皆為視窗的1/3,但要能隨著視窗大小更動
Jump to turn (10)
- User
請寫一個python視窗程式
- 大小800x600
- 放上九個文字方塊,以3x3排列,寬度與高度皆為視窗的1/3,但要能隨著視窗大小更動
- Assistant
要建立一個Python視窗程式,我們可以使用Tkinter這個內建的GUI庫。以下是根據您的需求建立的程式碼:
python
請寫一個python視窗程式
要建立一個Python視窗程式,我們可以使用Tkinter這個內建的GUI庫。以下是根據您的需求建立的程式碼:
import tkinter as tk
def main():
# 建立主視窗
root = tk.Tk()
root.title("3x3 文字方塊")
root.geometry("800x600")
root.minsize(300, 300)
# 建立九個文字方塊
text_boxes = []
for _ in range(9):
text_box = tk.Text(root, wrap=tk.WORD, bd=2)
text_boxes.append(text_box)
# 更新文字方塊的尺寸和位置
def update_text_boxes():
width = root.winfo_width()
height = root.winfo_height()
for i, text_box in enumerate(text_boxes):
row = i // 3
column = i % 3
x = width * column / 3
y = height * row / 3
box_width = width // 3
box_height = height // 3
text_box.place(x=x, y=y, width=box_width, height=box_height)
# 當視窗大小更動時,更新文字方塊的尺寸和位置
root.bind("<Configure>", lambda event: update_text_boxes())
# 啟動主循環
root.mainloop()
if __name__ == "__main__":
main()
這個程式將會建立一個800x600的視窗,並在其中放置九個文字方塊,以3x3排列。當視窗大小改變時,文字方塊的大小和位置也會自動調整。
. 字體放大一些 2. 9個文字方塊不要自動換行 3. 9個文字方塊加上右邊與下面的拉桿
根據您的要求,我將對程式碼進行修改:
import tkinter as tk
def main():
# 建立主視窗
root = tk.Tk()
root.title("3x3 文字方塊")
root.geometry("800x600")
root.minsize(300, 300)
# 建立九個文字方塊
text_boxes = []
for _ in range(9):
frame = tk.Frame(root, bd=2, relief=tk.SUNKEN)
text_box = tk.Text(frame, wrap=tk.NONE, font=("Arial", 14))
text_box.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
y_scrollbar = tk.Scrollbar(frame, orient=tk.VERTICAL, command=text_box.yview)
y_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
x_scrollbar = tk.Scrollbar(frame, orient=tk.HORIZONTAL, command=text_box.xview)
x_scrollbar.pack(side=tk.BOTTOM, fill=tk.X)
text_box.config(yscrollcommand=y_scrollbar.set, xscrollcommand=x_scrollbar.set)
text_boxes.append((text_box, frame))
# 更新文字方塊的尺寸和位置
def update_text_boxes():
width = root.winfo_width()
height = root.winfo_height()
for i, (_, frame) in enumerate(text_boxes):
row = i // 3
column = i % 3
x = width * column / 3
y = height * row / 3
box_width = width // 3
box_height = height // 3
frame.place(x=x, y=y, width=box_width, height=box_height)
# 當視窗大小更動時,更新文字方塊的尺寸和位置
root.bind("<Configure>", lambda event: update_text_boxes())
# 啟動主循環
root.mainloop()
if __name__ == "__main__":
main()
以下是修改部分的說明:
wrap=tk.WORD,將其設置為wrap=tk.NONE以取消自動換行。拉桿並沒有出現
對不起,我犯了一個錯誤。請在text_box.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)之前加上frame.pack_propagate(False)。以下是修正後的程式碼:
import tkinter as tk
def main():
# 建立主視窗
root = tk.Tk()
root.title("3x3 文字方塊")
root.geometry("800x600")
root.minsize(300, 300)
# 建立九個文字方塊
text_boxes = []
for _ in range(9):
frame = tk.Frame(root, bd=2, relief=tk.SUNKEN)
text_box = tk.Text(frame, wrap=tk.NONE, font=("Arial", 14))
frame.pack_propagate(False)
text_box.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
y_scrollbar = tk.Scrollbar(frame, orient=tk.VERTICAL, command=text_box.yview)
y_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
x_scrollbar = tk.Scrollbar(frame, orient=tk.HORIZONTAL, command=text_box.xview)
x_scrollbar.pack(side=tk.BOTTOM, fill=tk.X)
text_box.config(yscrollcommand=y_scrollbar.set, xscrollcommand=x_scrollbar.set)
text_boxes.append((text_box, frame))
# 更新文字方塊的尺寸和位置
def update_text_boxes():
width = root.winfo_width()
height = root.winfo_height()
for i, (_, frame) in enumerate(text_boxes):
row = i // 3
column = i % 3
x = width * column / 3
y = height * row / 3
box_width = width // 3
box_height = height // 3
frame.place(x=x, y=y, width=box_width, height=box_height)
# 當視窗大小更動時,更新文字方塊的尺寸和位置
root.bind("<Configure>", lambda event: update_text_boxes())
# 啟動主循環
root.mainloop()
if __name__ == "__main__":
main()
這個修正會讓每個文字方塊和滾動條正確地顯示在視窗中。在text_box.pack()之前加入frame.pack_propagate(False),以禁止框架尺寸根據其內容自動調整。
還是沒有拉桿
抱歉給您帶來困擾。經過再次檢查,我發現問題出在滾動條的顯示上。在這個修正的版本中,我使用了grid()替換pack(),以解決滾動條無法顯示的問題:
import tkinter as tk
def main():
# 建立主視窗
root = tk.Tk()
root.title("3x3 文字方塊")
root.geometry("800x600")
root.minsize(300, 300)
# 建立九個文字方塊
text_boxes = []
for _ in range(9):
frame = tk.Frame(root, bd=2, relief=tk.SUNKEN)
text_box = tk.Text(frame, wrap=tk.NONE, font=("Arial", 14))
text_box.grid(row=0, column=0, sticky=tk.NSEW)
y_scrollbar = tk.Scrollbar(frame, orient=tk.VERTICAL, command=text_box.yview)
y_scrollbar.grid(row=0, column=1, sticky=tk.NS)
text_box.config(yscrollcommand=y_scrollbar.set)
x_scrollbar = tk.Scrollbar(frame, orient=tk.HORIZONTAL, command=text_box.xview)
x_scrollbar.grid(row=1, column=0, sticky=tk.EW)
text_box.config(xscrollcommand=x_scrollbar.set)
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)
text_boxes.append((text_box, frame))
# 更新文字方塊的尺寸和位置
def update_text_boxes():
width = root.winfo_width()
height = root.winfo_height()
for i, (_, frame) in enumerate(text_boxes):
row = i // 3
column = i % 3
x = width * column / 3
y = height * row / 3
box_width = width // 3
box_height = height // 3
frame.place(x=x, y=y, width=box_width, height=box_height)
# 當視窗大小更動時,更新文字方塊的尺寸和位置
root.bind("<Configure>", lambda event: update_text_boxes())
# 啟動主循環
root.mainloop()
if __name__ == "__main__":
main()
現在滾動條應該可以正確顯示了。在這個版本中,我將文字方塊及滾動條的排列方法改為使用grid(),並將它們的行和列設置為可以拉伸以適應視窗大小。
個文字方塊需要新增儲存功能
根據您的要求,我在程式中添加了自動讀取、定時儲存和退出時儲存的功能。以下是修改後的程式碼:
import tkinter as tk
import os
import time
def read_file(file_path):
with open(file_path, "r", encoding="utf-8") as file:
return file.read()
def save_file(file_path, content):
with open(file_path, "w", encoding="utf-8") as file:
file.write(content)
def main():
# 建立主視窗
root = tk.Tk()
root.title("3x3 文字方塊")
root.geometry("800x600")
root.minsize(300, 300)
# 建立九個文字方塊
text_boxes = []
for i in range(9):
frame = tk.Frame(root, bd=2, relief=tk.SUNKEN)
text_box = tk.Text(frame, wrap=tk.NONE, font=("Arial", 14))
text_box.grid(row=0, column=0, sticky=tk.NSEW)
y_scrollbar = tk.Scrollbar(frame, orient=tk.VERTICAL, command=text_box.yview)
y_scrollbar.grid(row=0, column=1, sticky=tk.NS)
text_box.config(yscrollcommand=y_scrollbar.set)
x_scrollbar = tk.Scrollbar(frame, orient=tk.HORIZONTAL, command=text_box.xview)
x_scrollbar.grid(row=1, column=0, sticky=tk.EW)
text_box.config(xscrollcommand=x_scrollbar.set)
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)
text_boxes.append((text_box, frame))
file_name = f"text_box_{i+1}.txt"
if os.path.exists(file_name):
text_box.insert(tk.END, read_file(file_name))
# 更新文字方塊的尺寸和位置
def update_text_boxes():
width = root.winfo_width()
height = root.winfo_height()
for i, (_, frame) in enumerate(text_boxes):
row = i // 3
column = i % 3
x = width * column / 3
y = height * row / 3
box_width = width // 3
box_height = height // 3
frame.place(x=x, y=y, width=box_width, height=box_height)
# 儲存所有文字方塊的內容
def save_all():
for i, (text_box, _) in enumerate(text_boxes):
file_name = f"text_box_{i+1}.txt"
content = text_box.get(1.0, tk.END)
save_file(file_name, content)
# 定時儲存
def auto_save():
save_all()
root.after(60000, auto_save) # 每1分鐘執行一次
# 當視窗大小更動時,更新文字方塊的尺寸和位置
root.bind("<Configure>", lambda event: update_text_boxes())
# 在主視窗關閉前執行儲存操作
root.protocol("WM_DELETE_WINDOW", lambda: (save_all(), root.destroy()))
auto_save() # 啟動自動儲存功能
# 啟動主
繼續
循環 root.mainloop()
if name == "main": main()
這個修改後的程式碼包含以下功能:
1. 啟動時讀取對應的txt檔案,並將內容顯示在相應的文字方塊中。
2. 每隔1分鐘將每個文字方塊的內容儲存到對應的txt檔案中。
3. 在視窗關閉時,再次將每個文字方塊的內容儲存到對應的txt檔案中。
請注意,這個程式將在執行目錄下創建和讀取名為`text_box_1.txt`到`text_box_9.txt`的文件。
請再修正:
根據您的要求,我將視窗大小修改為1200x800,並將txt檔案放到"note"目錄下。以下是修改後的程式碼:
import tkinter as tk
import os
import time
def read_file(file_path):
with open(file_path, "r", encoding="utf-8") as file:
return file.read()
def save_file(file_path, content):
with open(file_path, "w", encoding="utf-8") as file:
file.write(content)
def main():
# 建立主視窗
root = tk.Tk()
root.title("3x3 文字方塊")
root.geometry("1200x800")
root.minsize(300, 300)
# 建立存儲目錄
if not os.path.exists("note"):
os.mkdir("note")
# 建立九個文字方塊
text_boxes = []
for i in range(9):
frame = tk.Frame(root, bd=2, relief=tk.SUNKEN)
text_box = tk.Text(frame, wrap=tk.NONE, font=("Arial", 14))
text_box.grid(row=0, column=0, sticky=tk.NSEW)
y_scrollbar = tk.Scrollbar(frame, orient=tk.VERTICAL, command=text_box.yview)
y_scrollbar.grid(row=0, column=1, sticky=tk.NS)
text_box.config(yscrollcommand=y_scrollbar.set)
x_scrollbar = tk.Scrollbar(frame, orient=tk.HORIZONTAL, command=text_box.xview)
x_scrollbar.grid(row=1, column=0, sticky=tk.EW)
text_box.config(xscrollcommand=x_scrollbar.set)
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)
text_boxes.append((text_box, frame))
file_name = f"note/text_box_{i+1}.txt"
if os.path.exists(file_name):
text_box.insert(tk.END, read_file(file_name))
# 更新文字方塊的尺寸和位置
def update_text_boxes():
width = root.winfo_width()
height = root.winfo_height()
for i, (_, frame) in enumerate(text_boxes):
row = i // 3
column = i % 3
x = width * column / 3
y = height * row / 3
box_width = width // 3
box_height = height // 3
frame.place(x=x, y=y, width=box_width, height=box_height)
# 儲存所有文字方塊的內容
def save_all():
for i, (text_box, _) in enumerate(text_boxes):
file_name = f"note/text_box_{i+1}.txt"
content = text_box.get(1.0, tk.END)
save_file(file_name, content)
# 定時儲存
def auto_save():
save_all()
root.after(60000, auto_save) # 每1分鐘執行一次
# 當視窗大小更動時,更新文字方塊的尺寸和位置
root.bind("<Configure>", lambda event: update_text_boxes())
# 在主視窗關閉前執行儲存操作
繼續
root.protocol("WM_DELETE_WINDOW", lambda: (save_all(), root.destroy()))
auto_save() # 啟動自動儲存功能
# 啟動主循環
root.mainloop()
if name == "main": main()
現在,視窗大小已更改為1200x800,並將txt檔案存放在"note"目錄下。如果"note"目錄不存在,程式將自動創建它。
請再修正:
根據您的要求,我將每個文字方塊的底色改為黑色,並為每個文字方塊設定不同的文字顏色。以下是修改後的程式碼:
import tkinter as tk
import os
import time
def read_file(file_path):
with open(file_path, "r", encoding="utf-8") as file:
return file.read()
def save_file(file_path, content):
with open(file_path, "w", encoding="utf-8") as file:
file.write(content)
def main():
# 建立主視窗
root = tk.Tk()
root.title("3x3 文字方塊")
root.geometry("1200x800")
root.minsize(300, 300)
# 建立存儲目錄
if not os.path.exists("note"):
os.mkdir("note")
# 文字顏色列表
text_colors = ["red", "orange", "yellow", "green", "blue", "purple", "pink", "cyan", "white"]
# 建立九個文字方塊
text_boxes = []
for i in range(9):
frame = tk.Frame(root, bd=2, relief=tk.SUNKEN)
text_box = tk.Text(frame, wrap=tk.NONE, font=("Arial", 14), bg="black", fg=text_colors[i])
text_box.grid(row=0, column=0, sticky=tk.NSEW)
y_scrollbar = tk.Scrollbar(frame, orient=tk.VERTICAL, command=text_box.yview)
y_scrollbar.grid(row=0, column=1, sticky=tk.NS)
text_box.config(yscrollcommand=y_scrollbar.set)
x_scrollbar = tk.Scrollbar(frame, orient=tk.HORIZONTAL, command=text_box.xview)
x_scrollbar.grid(row=1, column=0, sticky=tk.EW)
text_box.config(xscrollcommand=x_scrollbar.set)
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)
text_boxes.append((text_box, frame))
file_name = f"note/text_box_{i+1}.txt"
if os.path.exists(file_name):
text_box.insert(tk.END, read_file(file_name))
# 更新文字方塊的尺寸和位置
def update_text_boxes():
width = root.winfo_width()
height = root.winfo_height()
for i, (_, frame) in enumerate(text_boxes):
row = i // 3
column = i % 3
x = width * column / 3
y = height * row / 3
box_width = width // 3
box_height = height // 3
frame.place(x=x, y=y, width=box_width, height=box_height)
# 儲存所有文字方塊的內容
def save_all():
for i, (text_box, _) in enumerate(text_boxes):
file_name = f"note/text_box_{i+1}.txt"
content = text_box.get(1.0, tk.END)
save_file(file_name, content)
# 定時儲存
def auto_save():
save_all()
root.after(60000, auto_save) # 每1分鐘執行一次
# 當
繼續
視窗大小更動時,更新文字方塊的尺寸和位置 root.bind("", lambda event: update_text_boxes())
# 在主視窗關閉前執行儲存操作
root.protocol("WM_DELETE_WINDOW", lambda: (save_all(), root.destroy()))
auto_save() # 啟動自動儲存功能
# 啟動主循環
root.mainloop()
if name == "main": main()
現在,每個文字方塊的底色都已更改為黑色,並且每個文字方塊的文字顏色都是不同的。顏色列表中包含了9種顏色,您可以根據需要進行調整。
Not yet included in a published release.