0

In main.py there is class MainFrame which display Player._money In lands.py there is class Lands which allows the player to buy more lands. Inside class Lands, there is a function called def buy_lands(self) that respond to a button.

here's my problem: When buy_lands(self) is clicked, i've managed to update the label that shows the Player._lands as it's in the same class, same frame. However, the label that display the money is in a different frame, different class and a different file as it's in main.py class MainFrame.

How can i update Player._money in MainFrame from buy_lands(self) in class Lands without having a circular import error?

Here's the code if that's help:

main.py

import tkinter as tk 
from tkinter import ttk 
from lands import Lands
from player import Player

class MainFrame(ttk.Frame):
    def __init__(self, container):
        super().__init__(container)  
        
        options = {'padx': 5, 'pady': 5}

        self.player = Player._name

        self.__create_widgets()

        # show the frame on the container
        self.pack(**options)

    def __create_widgets(self):
        # Initialize style
        s = ttk.Style()

        # Frame Top Menu
        s.configure('Menu.TFrame', background='blue') # define the style
        self.menu = ttk.Frame(self, height=100, width=450, style='Menu.TFrame') # create the frame
        self.menu.pack() # place the frame

        # Main Frame  
        s.configure('Main.TFrame', background='red')
        self.main = ttk.Frame(self, height=300, width=300, style='Main.TFrame')
        self.main.pack()

        # Create Widgets
        self.name_label = ttk.Label(self.menu, text=f'Welcome {Player._name}')
        self.money_label =  ttk.Label(self.menu, text=f'money: {Player._money} £')
        self.button_1 = ttk.Button(self.menu, text="Button 1")
        self.lands_button = ttk.Button(self.menu, text="Lands", command=self.show_lands)
        self.button_3 = ttk.Button(self.menu, text="Button 3")
        self.button_4 = ttk.Button(self.menu, text="Button 4")

        # Display Widgets
        self.name_label.grid(column=0, columnspan=1, row=0, sticky='w')
        self.money_label.grid(column=2, row=0, columnspan=3, sticky='e')
        self.button_1.grid(column=0, row=1)
        self.lands_button.grid(column=1, row=1)
        self.button_3.grid(column=2, row=1)
        self.button_4.grid(column=3, row=1)


    def show_lands(self):
        for widget in self.main.winfo_children():
            widget.destroy()
        Lands(self.main)


lands.py

import tkinter as tk 
from tkinter import ttk
from player import Player


class Lands(ttk.Frame):
    def __init__(self, container):
      super().__init__(container)
      self.lands = Player._lands
      options = {'padx': 5, 'pady': 5}
      
    # show the frame on the container
      self.pack(**options)

      self.__create_widgets()


    def __create_widgets(self):
      self.lands_label = ttk.Label(self, text=f'You have {Player._lands} acres of lands')
      self.buy_lands_button = ttk.Button(self, text="Buy Lands", command=self.buy_lands)
      self.lands_label.pack()
      self.buy_lands_button.pack()
      

    def buy_lands(self):
        Player._money -= 5000
        Player._lands += 10
        self.lands_label.config(text=f'You have {Player._lands} acres of lands')
        
        self.lands_label.pack()
        print(Player._money)
        print(Player._lands)

i've tryed lambda methods but as i'm still learning i'm not too sure how to use it. I've tried global method but again because it's not in the same file, it doesn't work. And i tried to import MainFrame which shows a circular import error.

  • The rule is pretty simple: to modify something you need to have a reference to that something. That usually means you either need to pass the reference down to where it is needed, or provide an object or method that can return the reference. – Bryan Oakley Jan 11 '23 at 01:20
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Jan 11 '23 at 11:02

0 Answers0