Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter loops
#1
Hi guys!
I'm an absolute newbie in python.
I have this problem with tk inter:

class months():
    def __init__ (self,name,days_of_the_month,year):
        mesi.name = nome
        mesi.days = days_of_the_month
        mesi.year = year


january= months('January',31,2020)
february= months('February',29,2020)
.
.
.  
december= months('December',31,2020)

months_list = [january,february,........,december]

for i in range(len(months_list)):
    new_LabelFrame = LabelFrame(root,text= months_list[i].name)
    new_LabelFrame.pack()
    new_label = Label(new_LabelFrame,text ='Name: {}\nDays: {}\nYear: {}'.format(months_list[i].name,months_list[i].days,months_list[i].year))
    new_label.pack()
When i run the code, it geerates a windows with 12 LabelFrame containing only the December's month information. Could you please tell me why?
Reply
#2
We need to see your actual code. This code does not run because of errors (in __init__ change mesi to self and nome to name). When I fix the errors it displays different month information in each label.
Reply
#3
This is the complite code. It only show the December's attributes in every LabelFrame.
Sorry for the "mesi", it's the italian word for 'months'. I've translated the code to make it more comprehensible to you.

from tkinter import *
class months():
    def __init__ (self,name,days_of_the_month,year):
        months.name = name
        months.days = days_of_the_month
        months.year = year

root = Tk()
january= months('January',31,2020)
february= months('February',29,2020)
december= months('December',31,2020)

months_list = [january,february,december]

for i in range(len(months_list)):
    new_LabelFrame = LabelFrame(root,text= months_list[i].name)
    new_LabelFrame.pack()
    new_label = Label(new_LabelFrame,text ='Name: {}\nDays: {}\nYear: {}'.format(months_list[i].name,months_list[i].days,months_list[i].year))
    new_label.pack()

root.mainloop()
Reply
#4
See comments in the code below
import tkinter as tk  # dont flood name space with * imports


class Months():  # Capital M for class name
    def __init__(self, name, days_of_the_month, year):
        self.name = name  # self. not months.
        self.days = days_of_the_month  # self. not months.
        self.year = year  # self. not months.


root = tk.Tk()  # add tk.
january = Months('January', 31, 2020) # Capital M for class name
february = Months('February', 29, 2020) # Capital M for class name
december = Months('December', 31, 2020) # Capital M for class name

months_list = [january, february, december]

for item in months_list:  # iterate over the items directly
    new_LabelFrame = tk.LabelFrame(root, text=item.name)  # add tk., item.name
    new_LabelFrame.pack()
    new_label = tk.Label(new_LabelFrame, text='Name: {}\nDays: {}\nYear: {}'.format(
        item.name, item.days, item.year))  # add tk., item. name,days & year
    new_label.pack()

root.mainloop()
Reply
#5
ok Thanks!
Reply


Forum Jump:

User Panel Messages

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