我在python3中使用tkinter。我创建了一个die类作为canvas小部件的子类。然后,我创建了原始模具类的子类,它允许我冻结模具并防止它被卷起。最后,我创建了一个自定义小部件作为框架的子类,它包含freezable die和toggle freeze按钮。但是,即使我在按钮之前对模具画布进行网格化,按钮仍然会出现在画布上方。 (尝试手动控制行无效)以下是我的代码:
from tkinter import *
import random
class GUIDie(Canvas):
'''6-sided Die class for GUI'''
def __init__(self,master,valueList=[1,2,3,4,5,6],colorList=['black']*6):
'''GUIDie(master,[valueList,colorList]) -> GUIDie
creates a GUI 6-sided die
valueList is the list of values (1,2,3,4,5,6 by default)
colorList is the list of colors (all black by default)'''
# create a 60x60 white canvas with a 5-pixel grooved border
Canvas.__init__(self,master,width=60,height=60,bg='white',\
bd=5,relief=GROOVE)
# store the valuelist and colorlist
self.valueList = valueList
self.colorList = colorList
# initialize the top value
self.top = 1
def get_top(self):
'''GUIDie.get_top() -> int
returns the value on the die'''
return self.valueList[self.top-1]
def roll(self):
'''GUIDie.roll()
rolls the die'''
self.top = random.randrange(1,7)
self.draw()
def draw(self):
'''GUIDie.draw()
draws the pips on the die'''
# clear old pips first
self.erase()
# location of which pips should be drawn
pipList = [[(1,1)],
[(0,0),(2,2)],
[(0,0),(1,1),(2,2)],
[(0,0),(0,2),(2,0),(2,2)],
[(0,0),(0,2),(1,1),(2,0),(2,2)],
[(0,0),(0,2),(1,0),(1,2),(2,0),(2,2)]]
for location in pipList[self.top-1]:
self.draw_pip(location,self.colorList[self.top-1])
def draw_pip(self,location,color):
'''GUIDie.draw_pip(location,color)
draws a pip at (row,col) given by location, with given color'''
(centerx,centery) = (17+20*location[1],17+20*location[0]) # center
self.create_oval(centerx-5,centery-5,centerx+5,centery+5,fill=color)
def erase(self):
'''GUIDie.erase()
erases all the pips'''
pipList = self.find_all()
for pip in pipList:
self.delete(pip)
class GUIFreezeableDie(GUIDie):
'''a GUIDie that can be "frozen" so that it can't be rolled'''
def __init__(self,master,valueList=[1,2,3,4,5,6],colorList=['black']*6):
'''GUIFreezeableDie(master,[valueList,colorList]) -> GUIFreezeableDie
creates a GUI 6-sided freeze-able die
valueList is the list of values (1,2,3,4,5,6 by default)
colorList is the list of colors (all black by default)'''
# you add code here
GUIDie.__init__(self, master, valueList, colorList)
self.isFrozen = False
def is_frozen(self):
'''GUIFreezeableDie.is_frozen() -> bool
returns True if the die is frozen, False otherwise'''
return self.isFrozen
def toggle_freeze(self):
'''GUIFreezeableDie.toggle_freeze()
toggles the frozen status'''
if self.is_frozen():
self.isFrozen = False
self.configure(background = "white")
else:
self.isFrozen = True
self.configure(background = "grey")
def roll(self):
'''GuiFreezeableDie.roll()
overloads GUIDie.roll() to not allow a roll if frozen'''
if not self.is_frozen():
GUIDie.roll(self)
class GUIDieSet(Frame):
def __init__(self,master,valueList=[1,2,3,4,5,6],colorList=['black']*6):
'''GUIFreezeableDie(master,[valueList,colorList]) -> GUIFreezeableDie
creates a GUI 6-sided freeze-able die
valueList is the list of values (1,2,3,4,5,6 by default)
colorList is the list of colors (all black by default)'''
Frame.__init__(self,master)
self.grid()
self.die = GUIFreezeableDie(master, valueList, colorList)
self.die.grid()
self.toggleFreeze = Button(self,text='Freeze',command=self.die.toggle_freeze)
self.toggleFreeze.grid(row=1)
def roll(self):
self.die.roll()
class FreezeTest(Frame):
'''a small application to test the freezeable die'''
def __init__(self,master):
Frame.__init__(self,master)
self.grid()
self.die = GUIDieSet(self)
self.die.grid()
# test application
root = Tk()
test = TestFrame(root)
root.mainloop()
答案 0 :(得分:1)
在课程GUIDieSet()
中,删除self.grid()
中的行__init()__
,它似乎有效。