如何从另一个文件函数中获取值?蟒蛇

时间:2014-03-22 11:35:12

标签: python tkinter

有人可以帮忙吗? 我有两个名为game.pysettings.py的文件,我只想从游戏中使用的设置中获取一个值,但我不知道我做错了什么。

我希望它的值在函数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()

我在 create_ball_numbers

函数中需要 handling_settings 类中的值

这是我的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

如果我单独运行这两个文件......可以......但是当我尝试在bbbbb中的settings函数中获取值时,我会卡住...

2 个答案:

答案 0 :(得分:1)

你有一个循环导入; settings导入game,导入settings。那时,两个模块都没有完全初始化(超出import行的任何东西还没有运行)。

您实际上使用 game中的settings模块,因此只需从import game移除settings.py行。< / p>

game.py中,您导入了settings名称; bbbbb是该模块中 Application 类的属性。这一行:

self.yr = bbbbb()

永远不会在这里工作。

然而,您绝对应该 Tk()中创建新的settings根窗口;你只能在Tk应用程序中拥有一个主循环。使settings成为game中主应用程序触发的对话窗口。

因此,要获得bbbbb()结果,您需要生成设置对话框,让用户与其进行交互,然后在用户再次关闭对话框时检索ball_numbers设置。 / p>

答案 1 :(得分:0)

首先不要在settings.py中进行任何循环导入,不需要导入游戏模块。

由于bbbbb是类Application的函数,因此无法直接调用它,为了调用它我们需要一个同一个类的对象,它已经被创建为

app = Application(settings_window)

所以,在模块game.py中,只需使用app对象来调用bbbbb()

self.yr = settings.app.bbbbb()