Python 关于Tkinter-mainloop、更新方法、有效性等几个问题
背景:我正试图在Tkinter之上为python编写一个图形库。因此,我想从用户那里抽象Tkinter的所有功能,并使用方法调用以类似于处理的方式顺序修改根窗口。e、 g my library(我们称之为mylib)可以让您编写如下代码:Python 关于Tkinter-mainloop、更新方法、有效性等几个问题,python,tkinter,python-module,pyopengl,tkinter-canvas,Python,Tkinter,Python Module,Pyopengl,Tkinter Canvas,背景:我正试图在Tkinter之上为python编写一个图形库。因此,我想从用户那里抽象Tkinter的所有功能,并使用方法调用以类似于处理的方式顺序修改根窗口。e、 g my library(我们称之为mylib)可以让您编写如下代码: from mylib import * #this would be my library window(400, 400) #open a new window to add objects to color('red') #set the color of
from mylib import * #this would be my library
window(400, 400) #open a new window to add objects to
color('red') #set the color of all subsequent items to red
circle(radius=5, centerx = 0, centery=0) #make a red circle
line(10,10, 20, 20) #red line going from (10, 10) to (20, 20)
color('blue')
line(50, 50, 100, 100) #blue line
try:
from tkinter import *
except:
from Tkinter import *
_root = Tk()
class MyCanvas(Canvas):
def __init__(self, width=400, height=400):
master = Toplevel(_root)
Canvas.__init__(self, master, width=width, height=height)
self.pack()
self.items = []
self.width = width
self.height = height
_root.mainloop()
global _c
_c = None
def window():
_c = MyCanvas()
_c.pack()
def line(a,b,c,d):
#config code goes here
_c.create_line(a,b,c,d)
#test
window()
line(10, 10, 50, 50)
我想这样实施:
from mylib import * #this would be my library
window(400, 400) #open a new window to add objects to
color('red') #set the color of all subsequent items to red
circle(radius=5, centerx = 0, centery=0) #make a red circle
line(10,10, 20, 20) #red line going from (10, 10) to (20, 20)
color('blue')
line(50, 50, 100, 100) #blue line
try:
from tkinter import *
except:
from Tkinter import *
_root = Tk()
class MyCanvas(Canvas):
def __init__(self, width=400, height=400):
master = Toplevel(_root)
Canvas.__init__(self, master, width=width, height=height)
self.pack()
self.items = []
self.width = width
self.height = height
_root.mainloop()
global _c
_c = None
def window():
_c = MyCanvas()
_c.pack()
def line(a,b,c,d):
#config code goes here
_c.create_line(a,b,c,d)
#test
window()
line(10, 10, 50, 50)
这不起作用,因为Python无法脱离mainloop(),所以我尝试了以下方法:
尝试:
从tkinter进口*
除:
从Tkinter进口*
_root = Tk()
class MyCanvas(Canvas):
def __init__(self, width=400, height=400):
master = Toplevel(_root)
Canvas.__init__(self, master, width=width, height=height)
self.pack()
self.items = []
self.width = width
self.height = height
global _c
_c = None
def window():
_c = MyCanvas()
_c.pack()
def line(a,b,c,d):
#config code goes here
_c.create_line(a,b,c,d)
_root.update_idletasks()
#test
window()
line(10, 10, 50, 50)
但那也不起作用——它只是冻结了。我试着用update替换update_idletasks。窗户又结冰了。如何正确使用更新
有没有一种方法可以在不让用户显式调用mainloop()的情况下使用mainloop()完成此操作
或者有没有办法在mainloop中编辑小部件?我曾想过使用after,但我不知道如何解决这个问题
如果在这些约束条件下没有答案,那么在PyOpenGL中编写包是否有用或可移植?我应该从头开始使用C编写模块吗?还有别的吗?帮帮我
很抱歉问了这么长的问题。我已经做了几个小时了,但都没有用。在我看来,您最好将此作为tcl实现编写,并使用wish作为可执行文件。您似乎正在定义一种特定于域的语言(DSL),将其解释为驱动Tk的命令。这里似乎没有什么特别需要python的东西 你需要一个主回路。任何窗口应用程序都必须有一个事件泵,可以从系统中获取消息,并根据需要将它们发送到您的窗口。所有这类应用程序都必须在主UI线程仅泵送此消息队列的位置结束。对于Tk wish可执行文件,它内置于解释器中。然后运行事件循环。您可以在python库中通过加载客户机代码并作为after事件调用应用程序main函数来模拟这一点。您可能必须确保客户端代码不调用mainloop以避免冲突
如果您以前从未看过Tcl/Tk,您可能会想。您正在定义的内容与Tcl/Tk应用程序看起来并不遥远。简短回答:您需要mainloop。如何定义?更新工作,但只是停止响应。有没有一种方法可以线程化和处理callbakcs?使用_root.mainloop()作为脚本的最后一行…但我正在尝试将它制作成这样的库:它不使用mainloop,但可以编辑画布。我打算这样做。据我所知,他们似乎不使用mainloop。相反,他们将整个应用程序线程化,并使用更新。甚至Zelle的graphics.py