Jan-19-2021, 07:10 PM
I wrote the following program because I am trying to understand TKinter and classes.
I want the code to print "blue square" when I click the blue button. I want it to print "red circle" when I press the red button.
The buttons don't seem to do anything.
The program does print "blue square" and "red circle" and I don't understand why.
Why does the program print "blue square" and "red circle"? Why don't the buttons worK?
I want the code to print "blue square" when I click the blue button. I want it to print "red circle" when I press the red button.
The buttons don't seem to do anything.
The program does print "blue square" and "red circle" and I don't understand why.
Why does the program print "blue square" and "red circle"? Why don't the buttons worK?
import tkinter as tk root = tk.Tk() # from tkinter import ttk myfont = "helvitica 25" mybg = "light sky blue" myheight = 4 mywidth = 60 myheight2 = 6 mywidth2 = 20 myheight3 = 3 mywidth3 = 20 labcol = "spring green" anscol = "yellow" otherbutcol = "skyblue" commandbutcol = "light sky blue" mypadx =0 mypady=0 class mymenu: def __init__(self, root): self.root = root root.geometry('1200x1200') self.mainmenu() def mainmenu(self): self.mmframe = tk.Frame(root) self.mmframe.grid(row=0, column=0, sticky=tk.W + tk.E, padx=mypadx, pady=mypady) self.mb = tk.Button(self.mmframe, text="mybutton", bg=commandbutcol, font=myfont, relief="groove", command = colshape("bs")) self.mb.config(height=myheight3, width=mywidth2) self.mb.grid(row=2, column=1, sticky=tk.W + tk.E, padx=mypadx, pady=mypady) self.mob = tk.Button(self.mmframe, text="myotherbutton", bg="red", font=myfont, relief="groove", command =colshape("rc")) self.mob.config(height=myheight3, width=mywidth2) self.mob.grid(row=2, column=2, sticky=tk.W + tk.E, padx=mypadx, pady=mypady) def colshape(colshape): color = colshape[0] shape = colshape[1] if color == "r": color = "red" else: color = "blue" if shape == "s": shape = "square" else: shape = "circle" print(color, shape) m = mymenu(root) root.mainloop()