Python Forum
Why is this not working?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is this not working?
#1
from tkinter import *

class TheEarnings(Frame):

  def __init__(self, root=None):
    Frame.__init__(self)   
    self.root = root
    self.init_window()
    
    radioframe = Frame(root, width=120, height = 200, background="powder blue")
    self.radioframe.place(relx=.5, rely=.5, anchor="c")

  def init_window(self):
    self.root.title("The Ryall Family Earnings")
    self.pack(fill=BOTH, expand=1)
    self.create_radiobuttons()
  
  def create_radiobuttons(self):

    self.mode = IntVar()

    self.modetext = StringVar()
    self.modetext.set("Earnings")

    R1 = Radiobutton(radioframe, text="Assignments", variable=self.mode, value=1, command=self.string_table)
    R1.pack()

    R2 = Radiobutton(radioframe, text="Companies", variable=self.mode, value=2, command=self.string_table)
    R2.pack()

    R3 = Radiobutton(radioframe, text="Earnings", variable=self.mode, value=3, command=self.string_table)
    R3.pack()

    R4 = Radiobutton(radioframe, text="Employees", variable=self.mode, value=4, command=self.string_table)
    R4.pack()

    R5 = Radiobutton(radioframe, text="Programs", variable=self.mode, value=5, command=self.string_table)
    R5.pack()

    R6 = Radiobutton(radioframe, text="Rates", variable=self.mode, value=6, command=self.string_table)
    R6.pack()

    R7 = Radiobutton(radioframe, text="SpecialRateTypes", variable=self.mode, value=7, command=self.string_table)
    R7.pack()

    close_button = Button(radioframe, text="OK", command=self.quit)
    close_button.pack()

  def quit():
    self.quit()

  def string_table(self):
          
    self.val = self.mode.get()
    
    if self.val == 1:
      self.tbl = "Assignments"
    elif self.val == 2:
      self.tbl = "Companies"
    elif self.val == 3:
      self.tbl = "Earnings"
    elif self.val == 4:
      self.tbl = "Employees"
    elif self.val == 5:
      self.tbl = "Programs"
      print(self.tbl)
    elif self.val == 6:
      self.tbl = "Rates"
    elif self.val == 7:
      self.tbl = "SpecialRateTypes"

    global table
    table = self.tbl
    
root = Tk()
root.title("The Ryall Family Earnings")
root.geometry("1250x650")

#creation of an instance
app = TheEarnings(root)

root.mainloop()
How do I specify what frame the radionbuttons go into. I tried to specifying the self.radioframe frame that is defined in the the __init__ routine. But it’s not working. How do I specify what frame it’s in for radiobuttons. I’m attaching a screenshot and the radio buttons are supposed to be in the power blue frame. It seems that the radio bottons are outsider the powder blue frame.

PS I tried posting with the screen shot but it isn't working
Reply
#2
There are a bunch of reasons:
  • you were trying to run create radio buttons before you instantiated the radiobutton widget
  • you referred to radiobutton sometimes as self.radiobutton, and other times without self thus two instances neither of
    which existed.
  • self.quit called itself and quickly filled the recursion stack
  • also fixed code formatting

class TheEarnings(Frame):
    def __init__(self, root=None):
        Frame.__init__(self)
        self.root = root

        self.radioframe = Frame(root, width=120, height=200, background="powder blue")
        self.radioframe.place(relx=0.5, rely=0.5, anchor="c")

        self.init_window()

    def init_window(self):
        self.root.title("The Ryall Family Earnings")
        # self.pack(fill=BOTH, expand=1)
        self.create_radiobuttons()

    def create_radiobuttons(self):

        self.mode = IntVar()

        self.modetext = StringVar()
        self.modetext.set("Earnings")

        R1 = Radiobutton(
            self.radioframe,
            text="Assignments",
            variable=self.mode,
            value=1,
            command=self.string_table,
        )
        R1.pack()

        R2 = Radiobutton(
            self.radioframe,
            text="Companies",
            variable=self.mode,
            value=2,
            command=self.string_table,
        )
        R2.pack()

        R3 = Radiobutton(
            self.radioframe,
            text="Earnings",
            variable=self.mode,
            value=3,
            command=self.string_table,
        )
        R3.pack()

        R4 = Radiobutton(
            self.radioframe,
            text="Employees",
            variable=self.mode,
            value=4,
            command=self.string_table,
        )
        R4.pack()

        R5 = Radiobutton(
            self.radioframe,
            text="Programs",
            variable=self.mode,
            value=5,
            command=self.string_table,
        )
        R5.pack()

        R6 = Radiobutton(
            self.radioframe,
            text="Rates",
            variable=self.mode,
            value=6,
            command=self.string_table,
        )
        R6.pack()

        R7 = Radiobutton(
            self.radioframe,
            text="SpecialRateTypes",
            variable=self.mode,
            value=7,
            command=self.string_table,
        )
        R7.pack()

        close_button = Button(self.radioframe, text="OK", command=self.quit)
        close_button.pack()

    def quit(self):
        self.root.destroy()

    def string_table(self):

        self.val = self.mode.get()

        if self.val == 1:
            self.tbl = "Assignments"
        elif self.val == 2:
            self.tbl = "Companies"
        elif self.val == 3:
            self.tbl = "Earnings"
        elif self.val == 4:
            self.tbl = "Employees"
        elif self.val == 5:
            self.tbl = "Programs"
            print(self.tbl)
        elif self.val == 6:
            self.tbl = "Rates"
        elif self.val == 7:
            self.tbl = "SpecialRateTypes"

        global table
        table = self.tbl

def main():
    root = Tk()
    root.title("The Ryall Family Earnings")
    root.geometry("1250x650")

    # creation of an instance
    app = TheEarnings(root)
    root.mainloop()

if __name__ == '__main__':
    main()
[python]
from tkinter import *


class TheEarnings(Frame):
    def __init__(self, root=None):
        Frame.__init__(self)
        self.root = root

        self.radioframe = Frame(root, width=120, height=200, background="powder blue")
        self.radioframe.place(relx=0.5, rely=0.5, anchor="c")

        self.init_window()

    def init_window(self):
        self.root.title("The Ryall Family Earnings")
        # self.pack(fill=BOTH, expand=1)
        self.create_radiobuttons()

    def create_radiobuttons(self):

        self.mode = IntVar()

        self.modetext = StringVar()
        self.modetext.set("Earnings")

        R1 = Radiobutton(
            self.radioframe,
            text="Assignments",
            variable=self.mode,
            value=1,
            command=self.string_table,
        )
        R1.pack()

        R2 = Radiobutton(
            self.radioframe,
            text="Companies",
            variable=self.mode,
            value=2,
            command=self.string_table,
        )
        R2.pack()

        R3 = Radiobutton(
            self.radioframe,
            text="Earnings",
            variable=self.mode,
            value=3,
            command=self.string_table,
        )
        R3.pack()

        R4 = Radiobutton(
            self.radioframe,
            text="Employees",
            variable=self.mode,
            value=4,
            command=self.string_table,
        )
        R4.pack()

        R5 = Radiobutton(
            self.radioframe,
            text="Programs",
            variable=self.mode,
            value=5,
            command=self.string_table,
        )
        R5.pack()

        R6 = Radiobutton(
            self.radioframe,
            text="Rates",
            variable=self.mode,
            value=6,
            command=self.string_table,
        )
        R6.pack()

        R7 = Radiobutton(
            self.radioframe,
            text="SpecialRateTypes",
            variable=self.mode,
            value=7,
            command=self.string_table,
        )
        R7.pack()

        close_button = Button(self.radioframe, text="OK", command=self.quit)
        close_button.pack()

    def quit(self):
        self.root.destroy()

    def string_table(self):

        self.val = self.mode.get()

        if self.val == 1:
            self.tbl = "Assignments"
        elif self.val == 2:
            self.tbl = "Companies"
        elif self.val == 3:
            self.tbl = "Earnings"
        elif self.val == 4:
            self.tbl = "Employees"
        elif self.val == 5:
            self.tbl = "Programs"
            print(self.tbl)
        elif self.val == 6:
            self.tbl = "Rates"
        elif self.val == 7:
            self.tbl = "SpecialRateTypes"

        global table
        table = self.tbl

def main():
    root = Tk()
    root.title("The Ryall Family Earnings")
    root.geometry("1250x650")

    # creation of an instance
    app = TheEarnings(root)
    root.mainloop()

if __name__ == '__main__':
    main()
Reply
#3
you must change all radioframe to self.radioframe and do self.init_window() later


#!/usr/bin/python3
# -*- coding: utf-8 -*-
from tkinter import *
 
class TheEarnings(Frame):
 
  def __init__(self, root=None):
    Frame.__init__(self)   
    self.root = root
     
    self.radioframe = Frame(root, width=120, height = 200, background="powder blue")
    self.radioframe.place(relx=.5, rely=.5, anchor="c")

    self.init_window()
 
  def init_window(self):
    self.root.title("The Ryall Family Earnings")
    self.pack(fill=BOTH, expand=1)
    self.create_radiobuttons()
   
  def create_radiobuttons(self):
 
    self.mode = IntVar()
 
    self.modetext = StringVar()
    self.modetext.set("Earnings")
 
    R1 = Radiobutton(self.radioframe, text="Assignments", variable=self.mode, value=1, command=self.string_table)
    R1.pack()
 
    R2 = Radiobutton(self.radioframe, text="Companies", variable=self.mode, value=2, command=self.string_table)
    R2.pack()
 
    R3 = Radiobutton(self.radioframe, text="Earnings", variable=self.mode, value=3, command=self.string_table)
    R3.pack()
 
    R4 = Radiobutton(self.radioframe, text="Employees", variable=self.mode, value=4, command=self.string_table)
    R4.pack()
 
    R5 = Radiobutton(self.radioframe, text="Programs", variable=self.mode, value=5, command=self.string_table)
    R5.pack()
 
    R6 = Radiobutton(self.radioframe, text="Rates", variable=self.mode, value=6, command=self.string_table)
    R6.pack()
 
    R7 = Radiobutton(self.radioframe, text="SpecialRateTypes", variable=self.mode, value=7, command=self.string_table)
    R7.pack()
 
    close_button = Button(self.radioframe, text="OK", command=self.quit)
    close_button.pack()
 
  def quit():
    self.quit()
 
  def string_table(self):
           
    self.val = self.mode.get()
     
    if self.val == 1:
      self.tbl = "Assignments"
    elif self.val == 2:
      self.tbl = "Companies"
    elif self.val == 3:
      self.tbl = "Earnings"
    elif self.val == 4:
      self.tbl = "Employees"
    elif self.val == 5:
      self.tbl = "Programs"
      print(self.tbl)
    elif self.val == 6:
      self.tbl = "Rates"
    elif self.val == 7:
      self.tbl = "SpecialRateTypes"
 
    global table
    table = self.tbl
     
root = Tk()
root.title("The Ryall Family Earnings")
root.geometry("1250x650")
 
#creation of an instance
app = TheEarnings(root)
 
root.mainloop()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020