python - Tkinter: gridding canvas objects and buttons together -


i display 9 set cards in frame. under each card it's own button. i'm using setcard.deal_card(position) method place card in frame in 3x3 grid (position 0-8). when run program, buttons appear. when comment out pickbutton.grid() method, cards appear. don't know why can have 1 , not both.

from tkinter import * import random  class setcard(canvas):      def __init__(self,master,number,color,shape,shading):         # create 60x60 white canvas 5-pixel grooved border         canvas.__init__(self,master,width=100,height=160,bg='white',\                         bd=5,relief=raised) #also relief = sunken         # store valuelist , colorlist         self.number = number         self.color = color         self.shape = shape         self.shading = shading         self.selected=false         self.pickbutton=button(self,text="pick me!", command=self.toggle_card)      def deal_card(self,position):         '''puts given card position on 3x3 grid of frame'''         self.draw_card()         print("row: ",(position//3)*2)         print("col: ",position%3)         self.grid(row=(position//3)*2,column=position%3)         self.pickbutton.grid(row=((position//3)*2)+1,column= position %3)         self.master.deck.pop(0)      def toggle_card(self):         self.selected=true         self['bg']='gray'         self['relief']=sunken      def draw_card(self):         '''draws pips in correct locations on card'''         # clear old pips first         self.erase()         # location of pips should drawn         if self.number == 1:             self.draw_pip(position=2)         if self.number == 2:             self.draw_pip(position=1)             self.draw_pip(position=3)         if self.number == 3:             self.draw_pip(position=1)             self.draw_pip(position=2)             self.draw_pip(position=3)      def draw_pip(self,position):         '''draws single pip in given location'''         if self.shading=='solid':             inside=self.color         else:             inside=''         (topx,topy) = (55,10+ (position-1)*50)               if self.shape == 'diamond':             self.create_polygon(topx, topy, topx+15, topy+20, topx, topy+40, topx-15, topy+20,\                                 outline=self.color,fill=inside,width=3)             if self.shading=='stripe':                 incrementxy=[(5,30),(10,25), (15,20), (10,15), (5,10)]                 xy in incrementxy:                     self.create_line(topx-xy[0],topy+xy[1],topx+xy[0],topy+xy[1],fill=self.color)         if self.shape == 'circle':             self.create_oval(topx-20,topy,topx+20,topy+40,fill=inside,outline=self.color,width=3)             if self.shading=='stripe':                 incrementxy=[(10,3),(14,7),(17,10),(19,14),(20,20),(18,24),(17,30),(14,33),(10,37)]                 xy in incrementxy:                     self.create_line(topx-xy[0],topy+xy[1],topx+xy[0],topy+xy[1], fill= self.color)         if self.shape == 'square':             self.create_polygon(topx-20,topy,topx+20,topy,topx+20,topy+40,topx-20,topy+40,fill=inside,outline=self.color,width=3)             if self.shading=='stripe':                 increment in range(5,40,5):                     self.create_line(topx-20, topy+increment,topx+20,topy+increment, fill = self.color)       def erase(self):         '''erases pips'''         piplist = self.find_all()         pip in piplist:             self.delete(pip)  class setgameframe(frame):     '''frame game of set'''      def __init__(self,master):         '''creates new set frame'''         # set frame object         frame.__init__(self,master)         self.grid()         colorlist=['red','green','magenta']         shadinglist=['solid','stripe','open']         shapelist=['circle','diamond','square']         self.dealt_cards=[]         self.deck=[]         number in range(3):             color in colorlist:                 shading in shadinglist:                     shape in shapelist:                         self.deck.append(setcard(self,number+1,color,shape,shading))         random.shuffle(self.deck)         position in range(9):             self.deck[0].deal_card(position)             self.dealt_cards.append(self.deck[0])         self.setbutton=button(self,text="set!",command=self.pick_set)         self.setbutton.grid(row=6,column=1,rowspan=2)      def pick_set(self):         pass  def play_set():     root = tk()     root.title('set game')     game = setgameframe(root)     root.mainloop()  play_set() 

your mistake in making pick buttons slaves of cards instead of canvas.
change definition

self.pickbutton=button(master,text="pick me!", command=self.toggle_card)

and code work. what's happening code have it, card getting gridded in canvas, button getting gridded in card, , see button.

it seems natural (at least me) first parameter (master) self, since button attribute of card. however, widget hierarchy in tk (the gui toolkit underlying tkinter) has nothing this. first parameter here doesn't mean self.pickbutton child of master in objected-oriented sense, master (the canvas widget) managing self-pickbutton's "geometry".

by way, there's syntax error in code. @ line 30, there should quotation marks word "sunken."


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -