Python tkinter button isn't displaying -
i'm making card game (called monster master) develop python skills, oop.
i have gui has few static objects: player 1's side of table, player 2's side, wee line in middle , i'm trying implement 'end turn' button.
i have tried lot of different things try button display, can't appear if there no errors. saying there few commented out lines i've temporarily taken away sake of trying understand problem button.
here's code i'm using try:
def rungame(): class app(): """docstring app""" def draw(): # setting canvas dimensions canvas_width = 640 canvas_height = 480 master = toplevel() master.title("monster master charles cameron - game") master.resizable(width=false, height=false) master.geometry("640x480") w = canvas(master, width=canvas_width, height=canvas_height) w.pack() # drawing static objects centrepoints = [(0, canvas_height/2), (canvas_width/2, canvas_height/2), (canvas_width, canvas_height/2)] #left, centre , right centres (save me retyping them) player1area = w.create_rectangle(centrepoints[0], canvas_width, canvas_height, fill="#303afe") #player1 area player2area = w.create_rectangle(0, 0, centrepoints[2], fill="#c31b1b") #player2 area barrier = w.create_line(centrepoints[0], centrepoints[2], fill="#0090e3", width=20) # centre barrier # class gamebtn(): # class endturnbtn(): # def __init__(self, btnname, master): btnname = button(master, bg="white", command=lambda:print("clicked!")) image = imagetk.photoimage(file="imgs\endturn.png") btnname.config(image=image, width="70", height="70") btnname.pack(side=right) # changeturn = gamebtn.endturnbtn('changeturn', master) master.mainloop() window = app() app.draw()
the button code actual button worked fine when tried in own script stopped working when put inside program.
hope it's not dumb question ask, quite amateur @ python still , can't find answer anywhere online.
many thanks
your button exists, it's past edge of window. because made window 640x480, , filled 640x480 canvas. remove master.geometry("640x480")
line , window stretch contain both canvas , button.
you might thinking "but don't want button appear side of canvas. want button on canvas. canvas exists because wanted color sections of background". embedding widgets on canvas possible using create_window
(see how place widget in canvas widget in tkinter?), may more practical create colored backgrounds stacking frame objects , giving them individual colors. example:
from tkinter import tk, frame, button root = tk() root.geometry("640x480") top_player_frame = frame(root, height=230, bg="red") barrier = frame(root, height=20, bg="green") bottom_player_frame = frame(root, height = 230, bg="blue") #configure column 0 frames can stretch fit width of window root.columnconfigure(0, weight=1) top_player_frame.grid(row=0, sticky="we") barrier.grid(row=1, sticky="we") bottom_player_frame.grid(row=2, sticky="we") bottom_player_end_turn_button = button(bottom_player_frame, text="end turn") #use `place` here because `pack` or `grid` collapse frame tall widgets contains, i.e. button bottom_player_end_turn_button.place(x=10,y=10) root.mainloop()
result:
Comments
Post a Comment