CPU/Device/QTScreen.py
2024-04-29 16:01:52 +08:00

72 lines
2.4 KiB
Python

import tkinter
class DynamicPixelDisplay:
def __init__(self, width, height, pixel_size):
self.CANVAS_WIDTH = width
self.CANVAS_HEIGHT = height
self.PIXEL_SIZE = pixel_size
# 创建主窗口
self.root = tkinter.Tk()
self.root.title("Dynamic Pixel Display")
# 创建画布
self.canvas = tkinter.Canvas(
self.root, width=self.CANVAS_WIDTH, height=self.CANVAS_HEIGHT, bg="white"
)
self.canvas.pack()
# 创建一个二维列表来表示像素颜色,默认为白色
self.pixels = []
for y in range(self.CANVAS_HEIGHT // self.PIXEL_SIZE):
row = []
for x in range(self.CANVAS_WIDTH // self.PIXEL_SIZE):
rect = self.canvas.create_rectangle(
x * self.PIXEL_SIZE,
y * self.PIXEL_SIZE,
(x + 1) * self.PIXEL_SIZE,
(y + 1) * self.PIXEL_SIZE,
fill="white",
)
row.append(rect)
self.pixels.append(row)
# 设置初始屏幕内容
self.initial_screen_content = []
for _ in range(self.CANVAS_HEIGHT // self.PIXEL_SIZE):
row = ["white"] * (self.CANVAS_WIDTH // self.PIXEL_SIZE)
self.initial_screen_content.append(row)
def run(self):
self.root.mainloop()
# 修改像素颜色的方法
def set_pixel_color(self, x, y, color):
if (
0 <= x < self.CANVAS_WIDTH // self.PIXEL_SIZE
and 0 <= y < self.CANVAS_HEIGHT // self.PIXEL_SIZE
):
self.canvas.itemconfig(self.pixels[y][x], fill=color)
# 清空屏幕的方法
def clear_screen(self):
for y in range(self.CANVAS_HEIGHT // self.PIXEL_SIZE):
for x in range(self.CANVAS_WIDTH // self.PIXEL_SIZE):
self.set_pixel_color(x, y, "white")
# 用二维数组更新屏幕内容的方法
def update_screen(self, screen_content):
self.clear_screen() # 首先清空屏幕
# 根据二维数组的值更新屏幕内容
for y in range(len(screen_content)):
for x in range(len(screen_content[0])):
color = screen_content[y][x]
if isinstance(color, str):
self.set_pixel_color(x, y, color)
# 实例化DynamicPixelDisplay类
display = DynamicPixelDisplay(400, 400, 10)
display.run()