[英]tkinter canvas stays minimized
how do I get a canvas to actually have a size? 如何让画布实际上有一个尺寸?
root = Tk()
canv = Canvas(root, width=600, height=600)
canv.pack(fill = BOTH, expand = True)
root.after(1, draw)
mainloop()
just creates a window with a 1px canvas in the top left corner 只需创建一个左上角有1px画布的窗口
edit: I omitted draw, because it didn't throw anything, thus didn't seem relevant. 编辑:我省略了绘图,因为它没有抛出任何东西,因此似乎没有相关性。 But since it runs perfectly if I n through it via pdb, here's the full code:
但是,如果我通过pdb通过它完美运行,这里是完整的代码:
from tkinter import *
from pdb import set_trace
class Field: #abstact
def __init__(self, size):
super().__init__()
self.tiles = [[(None, 0) for i in range(size[1])] for j in range(size[0])] #list of columns
def get_tile(self, x,y):
return tiles[x][y]
def inc_tile(self, x,y,player):
t = self.tiles[x][y]
tiles[x][y] = (player, t[1])
class RectField(Field):
def __init__(self, size):
super().__init__(size)
def select_tile(self, x, y):
lx = len(self.tiles)
rx = floor(x*lx)
ly = len(self.tiles[rx])
ry = floor(x*ly)
return (rx, ry)
def draw(self, canvas):
canvas.delete(ALL)
w = canvas.winfo_width()
h = canvas.winfo_height()
canvas.create_rectangle(0, 0, w, h, fill='#f0f')
sx = w/len(self.tiles)
for i in range(len(self.tiles)):
sy = h/len(self.tiles[i])
for j in range(len(self.tiles[i])):
pl = self.tiles[i][j][0]
cl = '#888' if not pl else ('#f20' if pl == 1 else '#02f')
canvas.create_rectangle(i*sx, j*sy, (i+1)*sx, (j+1)*sy, fill=cl, outline='#000')
############################################################################## #####################
# MAIN
############################################################################## #####################
root = Tk()
canv = Canvas(root, width=600, height=600)
canv.pack(fill = BOTH, expand = True)
#set_trace()
field = RectField((4,4))
def init():
canv.create_rectangle(0, 0, 600, 600, fill='#f0f')
set_trace()
field.draw(canv)
root.update()
root.after(1, init)
mainloop()
OS is ubuntu 16.04, python version is the preinstalled python3, run via terminal OS是ubuntu 16.04,python版本是预装的python3,通过终端运行
Your canvas is not staying minimized. 你的画布没有保持最小化。 If you were to give the canvas a distinct background color you would see that it immediately fills the whole window, and stays that way.
如果你给画布一个明显的背景颜色,你会发现它会立即填满整个窗口,并保持这种状态。
You are getting 1
for the window width and height because you aren't giving tkinter enough time to draw it before asking for the size. 窗口宽度和高度都是
1
,因为在要求尺寸之前,你没有给tkinter足够的时间来绘制它。 winfo_width
and winfo_height
return the actual height, and the actual height can't be computed until the window is actually visible on the screen. winfo_width
和winfo_height
返回实际高度,直到窗口在屏幕上实际可见时才能计算实际高度。
The simplest solution is to force an update before calling your init
function. 最简单的解决方案是在调用
init
函数之前强制更新。 You can do this by calling update
and then calling init
rather than using after
. 您可以通过调用
update
然后调用init
而不是after
来执行此操作。
Instead of this: 而不是这个:
root.after(1, init)
Do this: 做这个:
root.update()
init()
I was able to get your canvas to show up and work fine. 我能够让你的画布显示出来并且工作正常。 It looks like your
init
function was the problem. 看起来你的
init
函数就是问题所在。 You don't need to define a time to wait when calling your init() function just call it directly and the program will do the rest. 调用init()函数时,您无需定义等待的时间,只需直接调用它,程序将完成剩下的工作。
Also I have looked over the tkinter documentation for canvas and I do not see anything like .draw()
I do not think a function like that exist for tkinter. 另外我查看了画布的
.draw()
文档,我没有看到像.draw()
这样的东西我认为.draw()
不存在这样的函数。 I could be wrong but if it does exist the documentation is not obvious. 我可能错了,但如果确实存在,那么文档并不明显。
use this instead: 改用它:
def init():
canv.create_rectangle(0, 0, 600, 600, fill='blue')
# I only changed the color to make sure it was working
#set_trace() # not needed.
#field.draw(canv) # does nothing?
#root.update() # not needed in this case.
init()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.