2017-02-09 45 views
0

Python3.4和Kivy 1.9.1問題與Python中,AttributeError的訪問窗口小部件Kivy: 'NoneType' 對象沒有屬性 '值'

我試圖加載(2)旋轉器窗口小部件from__init__,但是,我不斷收到以下錯誤:

Traceback Date: 02-09-2017, 11:44:10 
Traceback (most recent call last): 
    File "<string>", line 1, in <module> 
    File "C:\Python34x86\lib\idlelib\run.py", line 124, in main 
    ret = method(*args, **kwargs) 
    File "C:\Python34x86\lib\idlelib\run.py", line 351, in runcode 
    exec(code, self.locals) 
    File "C:\Python34x86\projects\rtpr_app\draft2\operator_rtpr_main.py", line 191, in <module> 
    OperatorRTPRApp().run() 
    File "C:\Python34x86\lib\site-packages\kivy\app.py", line 801, in run 
    self.load_kv(filename=self.kv_file) 
    File "C:\Python34x86\lib\site-packages\kivy\app.py", line 598, in load_kv 
    root = Builder.load_file(rfilename) 
    File "C:\Python34x86\lib\site-packages\kivy\lang.py", line 1842, in load_file 
    return self.load_string(data, **kwargs) 
    File "C:\Python34x86\lib\site-packages\kivy\lang.py", line 1921, in load_string 
    self._apply_rule(widget, parser.root, parser.root) 
    File "C:\Python34x86\lib\site-packages\kivy\lang.py", line 2082, in _apply_rule 
    child = cls(__no_builder=True) 
    File "C:\Python34x86\projects\rtpr_app\draft2\operator_rtpr_main.py", line 82, in __init__ 
    self.load_login_view() 
    File "C:\Python34x86\projects\rtpr_app\draft2\operator_rtpr_main.py", line 103, in load_login_view 
    self.spinner_part_number.values = split_line[2:] 
AttributeError: 'NoneType' object has no attribute 'values' 

我試圖在這裏找到了以下解決方案:Solution_1Solution_2

看來我引用kivy ID時,正確使用Python中它們有一個錯誤。請指出我的錯誤。

main.py

class LoginScreen(Screen): 
    spinner_bay = ObjectProperty()#ListProperty() 
    spinner_part_number = ObjectProperty()#ListProperty() 

    def __init__(self, **kwargs): 
     super(LoginScreen, self).__init__(**kwargs) 
     self.load_login_view() 

    def load_login_view(self): 
     print("Loading login screen ... " + strftime("%H:%M:%S")) 
     #Window.size = (300, 550) 

     PART_NUMBERS = []#ListProperty([]) 
     BAY_NUMBERS = []#ListProperty([]) 

     # Open configuration.txt file and find bay numbers & part numbers 
     try: 
      config_file = open('configuration.txt') 
      list_config_file = config_file.readlines() 
      #BAY_NUMBERS = bay_number_file.close 
      config_file.close 

      for line in list_config_file: 
       split_line = line.split(' ') 
       split_line[-1] = split_line[-1].strip() 

       if 'PART_NUMBERS' in split_line: 
        self.spinner_part_number.values = split_line[2:] 

       if 'BAY_NUMBERS' in split_line: 
        #for i in range(int(split_line[2]), int(split_line[3]) + 1, 1): 
        self.spinner_bay.values = split_line[2:] 
        print('Loaded bay numbers.') 
     except: 
      print("Error found.") 

.kv文件

#: import FadeTransition kivy.uix.screenmanager.FadeTransition 

ScreenManagement: 
    transition: FadeTransition() 
    LoginScreen: 
    #SimplifiedViewScreen: 
    SettingScreen: 

<LoginScreen>: 
    box_login:box_login 
    spinner_part_number: spinner_part_number 
    spinner_bay: spinner_bay 

    name: 'login' 

    on_enter: 
     root.load_login_view() 

    BoxLayout: 
     id: box_login 
     orientation: 'vertical' 
     padding: 10 

     canvas.before: 
      Color: 
       rgb: .082, .142, .138 

      Rectangle: 
       pos: self.pos 
       size: self.size 

      Color: 
       rgb: .151, .172, .156 

      Rectangle: 
       pos: (0, root.height-50) 
       size: (root.width,50) 
     Label: 
      text: 'A320neo RTPR' 
      size_hint: (1,0.25) 

     BoxLayout: 
      orientation: 'horizontal' 

      Label: 
       text: 'Order Number' 
       size_hint:(0.45, 1) 

      Label: 
       text: 'Part Number' 
       size_hint:(0.45, 1) 

     BoxLayout: 
      orientation: 'horizontal' 

      TextInput: 
       id: textinput_order_number 
       size_hint: (0.45, 1) 
       multiline: 'False' 
       on_text: root.validate_order_number() 

      Spinner: 
       id: spinner_part_number 
       size_hint: (0.45, 1) 

     BoxLayout: 
      orientation: 'horizontal' 

      Label: 
       text: 'Bay Number' 
       size_hint:(0.45, 1) 

     BoxLayout: 
      orientation: 'horizontal' 

      Spinner: 
       id: spinner_bay 
       size_hint: (0.45, 1) 

     BoxLayout: 
      orientation: 'horizontal' 

      Label: 
       text: 'Assembler 1' 
       size_hint:(0.45, 1) 

      Label: 
       text: 'Assembler 2' 
       size_hint:(0.45, 1) 

     BoxLayout: 
      orientation: 'horizontal' 

      TextInput: 
       id: textinput_assembler_1 
       size_hint: (0.45, 1) 
       multiline: 'False' 
       on_text: root.validate_assembler_1() 

      TextInput: 
       id: textinput_assembler_2 
       size_hint: (0.45, 1) 
       multiline: 'False' 
       on_text: root.validate_assembler_2() 

     Button: 
      text: 'LOGIN' 
      size_hint: (1, 1) 
      on_press: root.test() 

#<SimplifiedViewScreen>: 

<SettingScreen>: 
    name: 'settings' 

    on_enter: self.load_settings() 

    Label: 
     text: 'RTPR Settings' 

    Button: 
     on_release: app.root.current = 'simple' 
     text: 'Go Back' 
     font_size: 25 

回答

0

我終於想通了如何正確地呼籲蟒蛇ID帶有多個解決方案玩弄後:

上。 kv文件

創建id ch小部件的字符。例如:

class LoginScreen(Screen): 
    spinner_bay = ObjectProperty(None) 

改變該窗口小部件的方法如下::

Spinner: 
id: spinner_bay 
size_hint: (None, 1) 
width: root.width/5 

上.py文件

使用確切ID名稱給出創建OBJECTPROPERTY()

def test_spinner(self): 
    self.ids['spinner_bay_number'].values = ['Calibration', 'POU', 'Rework/Repairs'] 

和中提琴!

enter image description here

相關問題