Home > database >  if statements, TTK
if statements, TTK

Time:10-30

ytac=[]

import tkinter as tk
from tkinter import ttk


class App(tk.Tk):
  def __init__(self, title: str):
    super().__init__()
    self.title(title)

    self.style = ttk.Style(self)
    self.style.theme_use("classic") 

    self.frame0 = ttk.Frame(self, padding=(5,5))
    self.frame1 = ttk.Frame(self, padding=(5,5))
    self.frame2 = ttk.Frame(self, padding=(5,5))
    self.frame3 = ttk.Frame(self, padding=(5,5))

    self.initialize_frames()
    self.initialize_widgets()
    self.geometry("450x400")
  

    
  def initialize_widgets(self):
    self.uno_label.grid(row=0, column=0, sticky=tk.NSEW, padx=10, pady=10, columnspan=10)
    
  def make_frame0(self):
    self.coding_button = ttk.Button(self.frame0, text="Coding", command=self.get)
    self.coding_button.grid(row=1, column=5)

  def make_frame1(self):
    self.gamedev_button = ttk.Button(self.frame1, text="Game Development", command=self.get)
    self.gamedev_button.grid(row=1 ,column=5)

  def make_frame2(self):
    self.digitalmedia_button = ttk.Button(self.frame2, text="Digital Media", command=self.get)
    self.digitalmedia_button.grid(row=1, column=5)

  def make_frame3(self):
    self.uno_label = ttk.Label(self, text="Hello fellow Ytacer or so they call 'it. Ytac is a 4 week journey that allows to \ntake a Stem class of your choosing from a not so wide range of classes. \nIt's also unfourtenetly not sponsored by Dr.Pepper(f in the chat). 'Are you ready \nto start your expedition? What's 'my name you 'ask? I have 'no name but you \ncan call me Took. 'And also, Mel 'sucks and 'shouldn't be 'appreciated \nthroughout the rest of the cohort. *ahem*, they're 3 pathways to choose from,\n Coding(1), Game Development(2), Digital Media(3). Choose from one of these \nby clicking on the respective button. But before you do that I would like for us \nto introduce our sponsors RAID SHADOW LEGE-.") 
  

    
  def initialize_frames(self):
    self.make_frame0()
    self.make_frame1()
    self.make_frame2()
    self.make_frame3()

    self.frame0.grid(row=4, column=0)
    self.frame1.grid(row=4, column=1)
    self.frame2.grid(row=4, column=2)

  def get(self):
     if self.coding_button:
       print("ok")
     elif self.gamedev_button:
       print("poyo") 
     elif self.digitalmedia_button:
       print("GUH-HUH")
      

if __name__ == "__main__":
    app = App("YTAC")
    app.mainloop()

This def function will only print out the first string and not the other two even though I pressed all corresponding buttons. Reference

Expected corresponding string to print out after I pressed the corresponding button. I tried to change the elifs to their own if statements but that caused more problems.

CodePudding user response:

Without seeing all of the code I cannot accurately diagnose your problem, but it may lie in how you're using the Tkinter buttons.

Another question that seems similar to yours To Detect Button Press In Python Tkinter Module

A very thorough tutorial for Tkinter buttons: https://www.tutorialspoint.com/python/tk_button.htm

Essentially, Tkinter buttons are defined like so:

buttonName = tkinter.Button(master, option1=value1, option2=value1...)

Tkinter buttons have no "is pressed" variable, instead, you set the command (a function) that runs when the button is pressed.

buttonName = tkinter.Button(master, command=function_name, option1=value1, option2=value2...)

Note: Don't put parentheses after the function name as that will run the function. The only exception is if you have a function that returns a function

If you want a global variable that's changed when you push the button, a function like so works:

global_var = False
def callback():
    global global_var
    global_var = not global_var
button = tkinter.Button(master, command=callback)

In the above example, replace master with the name of the Tkinter window.

  • Related