打开 window 并在 Python 中绘制填充矩形的最简单方法
Easiest way to open a window and draw a filled rect on it in Python
我尝试了 pygame 和 tkinter,但它们都用 while 循环阻塞了主线程。有一些解决方法,但我认为它们相对复杂。例如,在 Java 中,我可以创建一个 JFrame,向其添加一个 JPanel 并在 JPanel 上绘制。这不会阻止创建 JFrame/JPanel 的线程。 Python 和 Java 之间是否有关键区别,以至于 Python 不能做同样的事情,或者我只是使用了错误的包或使用错误?
编辑1:
主要问题:在不阻塞主线程的情况下打开 window 并在其上绘制填充矩形的最简单方法 Python。
编辑2:
tkinter 示例:
import tkinter
root = tkinter.Tk()
canvas = tkinter.Canvas(root)
canvas.create_rectangle(10, 10, 60, 60, fill='blue')
canvas.pack()
tkinter.mainloop()
print("I won't get printed until window is closed")
pygame 示例:
import pygame
pygame.init()
width = 500
height = 500
window = pygame.display.set_mode((width, height))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
print("I won't get printed until window is closed")
我想要的:
class Canvas:
def __init__(self):
... # create window that does not block main thread
def draw_rect(self, x, y, width, height, color):
... # draw rect on window
def clear(self):
... # clear window
canvas = Canvas()
canvas.draw_rect(10, 10, 60, 60, 'blue')
print('I get printed even while window is active')
编辑 3:
我想要的 Java:
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class Canvas {
public static void main(String[] args) {
// test the Canvas class
Canvas canvas = new Canvas(500, 500); // creates canvas window
canvas.fillRect(50, 50, 200, 200, Color.BLUE); // draws a rect
canvas.clear(); // removes all drawings from canvas window
canvas.fillRect(50, 100, 300, 200, Color.RED);
canvas.fillRect(100,150,300,300, Color.CYAN);
// everything here will be executed
}
private JFrame frame = new JFrame();
private List<Consumer<Graphics>> drawTasks = new ArrayList<>();
public Canvas(int width, int height) {
JPanel panel = new JPanel() {
@Override
public void paint(Graphics g) {
super.paint(g);
for (Consumer<Graphics> drawTask : drawTasks) {
drawTask.accept(g);
}
}
};
panel.setPreferredSize(new Dimension(width, height));
frame.add(panel);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void fillRect(int x, int y, int width, int height, Color color){
drawTasks.add(graphics -> {
graphics.setColor(color);
graphics.fillRect(x, y, width, height);
});
frame.repaint();
}
public void clear() {
drawTasks.clear();
frame.repaint();
}
}
当您 运行 您的 Java 代码时,会创建一个单独的线程(事件调度线程)来为您处理 GUI。 pygame 中的等价物是这样的:
import pygame
import threading
class Canvas(threading.Thread):
def __init__(self):
super().__init__()
self.screen = pygame.display.set_mode((800, 600))
self.screen.fill((50, 50, 50))
self.clock = pygame.time.Clock()
self.start()
def draw_rect(self, x, y, width, height, color):
pygame.draw.rect(self.screen, pygame.Color(color), (x, y, width, height))
def clear(self):
self.screen.fill((50, 50, 50))
def run(self):
while True:
for e in pygame.event.get():
print(e)
if e.type == pygame.QUIT:
return
pygame.display.update()
self.clock.tick(60)
canvas = Canvas()
canvas.draw_rect(10, 10, 60, 60, 'blue')
print('I get printed even while window is active')
但是,正如您在 运行 这段代码中看到的那样,它不会工作,因为 pygame 的事件系统只有在从主线程调用时才能正常工作(至少在Windows。它适用于 Linux IIRC)。
因此,您所要求的 pygame 这种方式并不可靠。
使用 tkinter,你可以使用这样的东西:
import threading
class Canvas(threading.Thread):
def __init__(self):
super().__init__()
self.start()
def run(self):
import tkinter
root = tkinter.Tk()
s = tkinter.StringVar()
s.set('Foo')
f = tkinter.StringVar()
f.set('Bar')
tkinter.Label(root,textvariable=s).pack()
tkinter.Button(root,textvariable=f).pack()
root.mainloop()
app = Canvas()
print('I get printed even while window is active')
这会起作用,但有点老套,因为 tkinter
模块必须导入到您调用 mainloop()
.
的同一个线程中
我尝试了 pygame 和 tkinter,但它们都用 while 循环阻塞了主线程。有一些解决方法,但我认为它们相对复杂。例如,在 Java 中,我可以创建一个 JFrame,向其添加一个 JPanel 并在 JPanel 上绘制。这不会阻止创建 JFrame/JPanel 的线程。 Python 和 Java 之间是否有关键区别,以至于 Python 不能做同样的事情,或者我只是使用了错误的包或使用错误?
编辑1: 主要问题:在不阻塞主线程的情况下打开 window 并在其上绘制填充矩形的最简单方法 Python。
编辑2:
tkinter 示例:
import tkinter
root = tkinter.Tk()
canvas = tkinter.Canvas(root)
canvas.create_rectangle(10, 10, 60, 60, fill='blue')
canvas.pack()
tkinter.mainloop()
print("I won't get printed until window is closed")
pygame 示例:
import pygame
pygame.init()
width = 500
height = 500
window = pygame.display.set_mode((width, height))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
print("I won't get printed until window is closed")
我想要的:
class Canvas:
def __init__(self):
... # create window that does not block main thread
def draw_rect(self, x, y, width, height, color):
... # draw rect on window
def clear(self):
... # clear window
canvas = Canvas()
canvas.draw_rect(10, 10, 60, 60, 'blue')
print('I get printed even while window is active')
编辑 3:
我想要的 Java:
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class Canvas {
public static void main(String[] args) {
// test the Canvas class
Canvas canvas = new Canvas(500, 500); // creates canvas window
canvas.fillRect(50, 50, 200, 200, Color.BLUE); // draws a rect
canvas.clear(); // removes all drawings from canvas window
canvas.fillRect(50, 100, 300, 200, Color.RED);
canvas.fillRect(100,150,300,300, Color.CYAN);
// everything here will be executed
}
private JFrame frame = new JFrame();
private List<Consumer<Graphics>> drawTasks = new ArrayList<>();
public Canvas(int width, int height) {
JPanel panel = new JPanel() {
@Override
public void paint(Graphics g) {
super.paint(g);
for (Consumer<Graphics> drawTask : drawTasks) {
drawTask.accept(g);
}
}
};
panel.setPreferredSize(new Dimension(width, height));
frame.add(panel);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void fillRect(int x, int y, int width, int height, Color color){
drawTasks.add(graphics -> {
graphics.setColor(color);
graphics.fillRect(x, y, width, height);
});
frame.repaint();
}
public void clear() {
drawTasks.clear();
frame.repaint();
}
}
当您 运行 您的 Java 代码时,会创建一个单独的线程(事件调度线程)来为您处理 GUI。 pygame 中的等价物是这样的:
import pygame
import threading
class Canvas(threading.Thread):
def __init__(self):
super().__init__()
self.screen = pygame.display.set_mode((800, 600))
self.screen.fill((50, 50, 50))
self.clock = pygame.time.Clock()
self.start()
def draw_rect(self, x, y, width, height, color):
pygame.draw.rect(self.screen, pygame.Color(color), (x, y, width, height))
def clear(self):
self.screen.fill((50, 50, 50))
def run(self):
while True:
for e in pygame.event.get():
print(e)
if e.type == pygame.QUIT:
return
pygame.display.update()
self.clock.tick(60)
canvas = Canvas()
canvas.draw_rect(10, 10, 60, 60, 'blue')
print('I get printed even while window is active')
但是,正如您在 运行 这段代码中看到的那样,它不会工作,因为 pygame 的事件系统只有在从主线程调用时才能正常工作(至少在Windows。它适用于 Linux IIRC)。
因此,您所要求的 pygame 这种方式并不可靠。
使用 tkinter,你可以使用这样的东西:
import threading
class Canvas(threading.Thread):
def __init__(self):
super().__init__()
self.start()
def run(self):
import tkinter
root = tkinter.Tk()
s = tkinter.StringVar()
s.set('Foo')
f = tkinter.StringVar()
f.set('Bar')
tkinter.Label(root,textvariable=s).pack()
tkinter.Button(root,textvariable=f).pack()
root.mainloop()
app = Canvas()
print('I get printed even while window is active')
这会起作用,但有点老套,因为 tkinter
模块必须导入到您调用 mainloop()
.