Reputation: 93
Does anyone can help here?
I have two files called game.py
and settings.py
, I just want to get one value from settings to use in game, but I dont know what I am doing wrong.
the value I want it is in the function bbbbb...
THIS IS MY SETTINGS
from tkinter import*
import game
class Application(Frame):
def __init__ (self, master):
Frame.__init__(self,master)
self.grid()
self.create_widgets()
def bbbbb(self):
self.xr = self.ball_numbers.get()
print("printing...", self.xr)
return self.xr
def create_widgets(self):
self.ball_numbers = IntVar()
Label(self,text = "Select how many balls you wish to play:").grid()
Radiobutton(self, text = "1 Ball", variable = self.ball_numbers, value = 1, command = self.bbbbb).grid ()
Radiobutton(self, text = "2 Balls", variable = self.ball_numbers, value = 2, command = self.bbbbb).grid ()
Radiobutton(self, text = "3 Balls", variable = self.ball_numbers, value = 3, command = self.bbbbb).grid ()
settings_window = Tk()
settings_window.title(" THE BOUNCER - Settings")
settings_window.geometry("600x600")
app = Application(settings_window)
settings_window.mainloop()
I need that value in the class handling_settings, in the function create_ball_numbers
AND THIS IS MY game.py
from livewires import games, color
from tkinter import*
import settings
import random
games.init(screen_width = 735, screen_height = 350, fps = 35)
class Bounce(games.Sprite):
def update(self):
if self.right > games.screen.width or self.left < 0:
self.dx = -self.dx
if self.top < 0:
self.dy = -self.dy
if self.bottom == 315 and self.overlapping_sprites:
self.dy = -self.dy
class Bar_moving(games.Sprite):
def update(self):
self.x = games.mouse.x
self.y = 315
class handling_settings():
self.yr = bbbbb()
print("printing number from settings ", self.yr)
def create_ball_numbers(self):
print("inside def", self.yr)
def main():
background = games.load_image("BG.jpg", transparent = False)
games.screen.background = background
call = handling_settings()
call.create_ball_numbers()
bar_small = games.load_image("bar_small.jpg", transparent = False)
the_bar_small = Bar_moving(image = bar_small, x = games.mouse.x)
games.screen.add(the_bar_small)
games.mouse.is_visible = False
games.screen.event_grab = True
games.screen.mainloop()
main()
I think I am not using on the right way IMPORT on top of the file.... keeping appearing this msg...
File "C:\Users\Bruno\Desktop\DIT\Object Oriented Programming\GAME - Assignment\game.py", line 3, in <module>
from settings import bbbbb
ImportError: cannot import name bbbbb
If I run both files individually... its ok... but when I try to get the value in bbbbb
function in settings
, I get stuck...
Upvotes: 0
Views: 1811
Reputation: 1122
First don't do any circular import, in settings.py there is no need to import game module.
As bbbbb is a function of class Application, therefore it cannot be called directly, to call it we need an object of the same class, which is already created as
app = Application(settings_window)
so, in module game.py, just use app object to call bbbbb()
self.yr = settings.app.bbbbb()
Upvotes: 0
Reputation: 1121834
You have a circular import; settings
imports game
, which imports settings
. At that time, neither module is fully done initializing (anything beyond the import
lines has not yet run).
You don't actually use the game
module in settings
, so just remove the import game
line from settings.py
.
In game.py
, you imported the settings
name; bbbbb
is an attribute on the Application
class in that module. The line:
self.yr = bbbbb()
will never work here.
You should definitely not create a new Tk()
root window in settings
, however; you can only ever have one main loop in a Tk application. Make settings
a dialog window triggered by the main application in game
.
So to get the bbbbb()
result, you need to instead spawn the settings dialog box, let the user interact with it, and then retrieve the ball_numbers
setting when the user closes the dialog again.
Upvotes: 1