如何调整GraphWin窗口大小?

2 投票
3 回答
4426 浏览
提问于 2025-04-18 14:22

我正在使用Zelle的图形库来完成一些在线课程作业。我的作业中有一部分似乎假设我可以调整一个已经存在的GraphWin窗口的大小。但是在课程中之前并没有提到过这个内容,我查看了graphics.py的文档,发现没有办法做到这一点。我试着研究了一下GraphWin对象,发现没有什么可以改变窗口大小的选项。请问,是否可以调整GraphWin窗口的大小呢?

我尝试过:

from graphics import *
new_win = GraphWin('Test', 300, 300)
new_win.setCoords(0, 0, 100, 200)
new_win.width = 100

3 个回答

0

我刚刚发现怎么做这个了

from graphics import *
win= GraphWin("Person",400,400)
2

setCoords() 方法的作用是创建一个新的虚拟坐标系统,这个坐标系统是在一个已经存在的窗口里。

我们可以通过直接使用 tkinter 的功能,来实现你需要的功能,特别是对 GraphWin 进行一些调整:

from graphics import *

class ResizeableGraphWin(GraphWin):

    """ A resizeable toplevel window for Zelle graphics. """

    def __init__(self, title="Graphics Window", width=200, height=200, autoflush=True):
        super().__init__(title, width, height, autoflush)
        self.pack(fill="both", expand=True)  # repack?

    def resize(self, width=200, height=200):
        self.master.geometry("{}x{}".format(width, height))
        self.height = int(height)
        self.width = int(width)

# test code

win = ResizeableGraphWin("My Circle", 100, 100)
win.setBackground('green')

c = Circle(Point(75, 75), 50)
c.draw(win)  # should only see part of circle

win.getMouse() # pause for click in window

win.resize(200, 400)  # should now see all of circle

win.getMouse() # pause for click in window

c.move(25, 125)  # center circle in newly sized window

win.getMouse() # pause for click in window

c.setFill('red')  # modify cirlce

win.getMouse() # pause for click in window

win.close()

这是一个 Python 3 的实现,因为我用了 super()。这个代码可能也能改成适用于 Python 2 的版本。

1

Zelle的图形库没有提供一个可以在窗口绘制后调整窗口大小的方法。

撰写回答