2017-05-06 87 views
0

我有一个代码,理想情况下应该有一个4行16列的键盘设计。 OSK按钮和画布 - Python的tkinter

正如你在照片中看到的那样,有一个'A'按钮已经熄灭。它应该在右上角。任何人都可以告诉我代码有什么问题吗?

我知道我已经硬编码索引值作为一个数组,但这就是我如何开始与脚本。一旦我使脚本按预期工作,我会将它们更改为列表/范围的组合。另外,如果我在代码中的任何评论都是错误的,请纠正我。我对tkinter很陌生。

代码:

import tkinter 

def func(): 
    print("Clicked") 

# GUI window object 
top = tkinter.Tk() 

# Canvas object 
C = tkinter.Canvas(top, bg="black", height=312, width=778) 

# Specifying the coords of the keyboard 
# Coords borders: 
# y => 0-74, 76-160, 162-236, 238-312 
# x => 0-50, 52-102, 104-154, 156-206, 
# 208-258, 260-310, 312-362, 364-414, 
# 416-466, 468-518, 520-570, 572-622, 
# 624-674, 676-726, 728-778 
y = (312,238,236,162,160,76,74,0) 
x = (0,50,52,102,104,154,156,206,208,258,260,310,312,362,364,414,416,466,468,518,520,570,572,622,624,674,676,726,728,778) 
by = (285, 199, 123, 37) 
bx = (25,77,129,181,233,285,337,389,441,493,545,597,649,701,753) 

yi = (0,2,4,6) 
xi = (0,2,4,6,8,10,12,14,16,18,20,22,24,26,28) 
biy = (0,1,2,3) 
bix = (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14) 

for indexY in yi: 
    for indexX in xi: 
     #print ("Xi = " + str(x[indexX]) + " Xi+1 = " + str(x[indexX+1]) + " Yi = " + str(y[indexY]) + " Yi+1 = " + str(y[indexY+1])) 
     coord = x[ indexX ], y[ indexY ], x[ indexX ], y[ indexY + 1 ], x[ indexX + 1 ], y[ indexY + 1 ], x[ indexX + 1 ], y[ indexY ] 
     # Create polygon object 
     oval = C.create_polygon(coord, fill="white") 

for indexY in biy: 
    for indexX in bix: 
     #print (by[indexY]) 
     #print (bx[indexX]) 
     B = tkinter.Button(top, text = 'A', command = func) 
     B1 = C.create_window(bx[ indexX ] , by[ indexY ], window = B) 

# Combine all canvas objects 
C.pack() 

# Combine all button objects 
B.pack() 

# Runs an infinite loop so that the resultant window doesn't disappear 
# Executed only once when application is ready 
top.mainloop() 

回答

0

的问题是线B.pack() - 在这一点上B是一个按钮,最后创建(因此该行您的评论是完全错误的)的人。打包将其从原先放置在其中的画布的控制中移开,并将其作为独立项目放置在其父控件中。

+0

删除'B.pack()'解决了这个问题。谢谢! –