从Spinner on_text问题更改屏幕

时间:2019-08-30 18:20:33

标签: python-3.x kivy spinner

我想使用ScreenManager创建一种工作流程,该工作流程会随着选项的选择而进展。我在ScreenManager中定义了多个屏幕,并且在一个屏幕中创建了一个带有多个选项的微调器。我想从on_text调用一个Function并传递所选的Spinner值,基于传递的值执行任务,然后移至工作流中的下一个屏幕。

我已经在类(屏幕)内创建了一个仅带有print()的功能用于测试。当我从微调器中选择一个选项时,没有任何反应。

我只提供了我认为是相关的代码...

kv文件:

<HomeScreen>:
    name: 'homeScreen'
    BoxLayout:
        orientation: 'horizontal'

        Label:
            id: 'home'
            text: 'Home Screen'


<NewSession>:
    name: 'newSession'
    BoxLayout:
        orientation: 'vertical'

        Label:
            id: new
            text: 'Create New Session'

        Spinner:
            size_hint: None, None
            size: 100, 25
            id: category
            font_size: 12
            text: "Food Category" #default value showed
            values: ["Beef","Pork","Poultry", "Fish"] #list of values to show
            on_text: root.SelectCut(category.text)

        Widget:


<CurrentSession>:
    id: cs
    name: 'currentSession'
    BoxLayout:
        orientation: 'vertical'

        Label:
            id: cSession
            text: root.cSession

<History>:
    name: 'history'
    BoxLayout:
        orientation: 'vertical'

        Label:
            id: 'history'
            text: 'History Data Screen'

<ScreenManagement>:
    HomeScreen:
    NewSession:
    CurrentSession:
    History:

<AllScreen>:

    orientation: 'vertical'
    ScreenManagement:
        id: sm
    BoxLayout:

        size_hint_y: None
        height: 60
        spacing: 5
        padding: 5, 5, 0, 5

        Button:
            text: 'Home'
            on_press: root.ids.sm.current = 'homeScreen'
        Button:
            text: 'New Session'
            on_press: root.ids.sm.current = 'newSession'
        Button:
            text: 'Current Session'
            on_press: 
                root.ids.sm.current = 'currentSession'
        Button:
            text: 'History'
            on_press: root.ids.sm.current = 'history'

py文件:

Builder.load_file("screenLayout.kv")

class ScreenManagement(ScreenManager):
    pass

class HomeScreen(Screen):
    pass

class NewSession(Screen):

    def SelectCut(self, text):
        print("In Select Cut")
        print("Food Category: " + text)
        self.sm.current = 'homeScreen'

class CurrentSession(Screen):
    pass

class History(Screen):
    pass

class AllScreen(BoxLayout):
    pass

class WorkingTestApp(App):

    def build(self):
        self.root = AllScreen()
        return self.root

if __name__ == '__main__':
    WorkingTestApp().run()

我无法弄清楚为什么从微调器中进行选择时什么也没发生,而且我不确定从函数中移到下一个屏幕时的语法是什么。


更新: 当前代码(减去进口)

kv:

<HomeScreen>:
    name: 'homeScreen'
    BoxLayout:
        orientation: 'horizontal'

        Label:
            id: 'home'
            text: 'Home Screen'

<NewSession>:
    name: 'newSession'
    BoxLayout:
        orientation: 'vertical'

        Label:
            id: new
            text: 'Create New Session'

        Spinner:
            size_hint: None, None
            size: 100, 25
            id: category
            font_size: 12
            text: "Food Category" #default value showed
            values: ["Beef","Pork","Poultry", "Fish"] #list of values to show
            on_text: root.SelectCategory()

        Widget:

<SelectCut>:
    name: 'selectCut'
    BoxLayout:
        orientation: 'vertical'

        Label:
            id: 'cut'
            text: 'Select Cut Screen'

        Widget:

<CurrentSession>:
    name: 'currentSession'
    BoxLayout:
        orientation: 'vertical'

        Label:
            id: cSession
            text: 'Current Session Screen'

<History>:
    name: 'history'
    BoxLayout:
        orientation: 'vertical'

        Label:
            id: 'history'
            text: 'History Data Screen'

<ScreenManagement>:
    HomeScreen:
    NewSession:
    SelectCut:
    CurrentSession:
    History:

<AllScreen>:
    orientation: 'vertical'
    ScreenManagement:
        id: sm
    BoxLayout:

        size_hint_y: None
        height: 60
        spacing: 5
        padding: 5, 5, 0, 5

        Button:
            text: 'Home'
            on_press: root.ids.sm.current = 'homeScreen'
        Button:
            text: 'New Session'
            on_press: root.ids.sm.current = 'newSession'
        Button:
            text: 'Current Session'
            on_press: 
                root.ids.sm.current = 'currentSession'
        Button:
            text: 'History'
            on_press: root.ids.sm.current = 'history'

py:

Builder.load_file("screenLayout.kv")

class ScreenManagement(ScreenManager):
    pass

class HomeScreen(Screen):
    pass

class NewSession(Screen):
    # This Function isn't executing consistently.
    def SelectCategory(self):
        print("Food Category: " + self.ids.category.text)
        self.manager.current = 'selectCut'

class SelectCut(Screen):
    pass

class CurrentSession(Screen):
    pass

class History(Screen):
    pass

class AllScreen(BoxLayout):
    pass

class WorkingTestApp(App):

    def build(self):
        self.root = AllScreen()
        return self.root

if __name__ == '__main__':
    WorkingTestApp().run()

1 个答案:

答案 0 :(得分:0)

将微调器中的# Filename: convert_str_orch_to_piano_musescore_bvw1048_music21_py3.py # Function: Sample of how to convert sheetmusic in MuseScore format with seperate voices # using BWV1048 3rd Brandeburger Concert 1st movement, # to sheetmusic for piano # Comment: For Music21 documentation see: https://web.mit.edu/music21/doc/index.html # For samples see https://github.com/cuthbertLab/music21-tools import music21 as m scorePath = "~/Documents/sources/python/python3/python3_music21" # Export de MuseScore File in musicxml (uncompressed music mxl format) museScoreFile = "BWV1048_Brandenburg_Concerto_No_3_in_G_Major_in_parts_orgineel_1st_mov.musicxml" # Set Meta Data composer = 'J.S. Bach (1685 - 1750' title = museScoreFile outputFormat = 'musicxml' outputFileDFLT = scorePath+'/output.'+outputFormat # See: https://web.mit.edu/music21/doc/usersGuide/usersGuide_24_environment.html#usersguide-24-environment env = m.environment.UserSettings() env.delete() env.create() # set environmment env['autoDownload'] = 'allow' env['lilypondPath'] = '/usr/bin/lilypond' env['musescoreDirectPNGPath'] = '/usr/bin/musescore3' env['musicxmlPath'] = '/usr/bin/musescore3' curr_stream = m.converter.parse(scorePath+'/'+museScoreFile, format='musicxml') # curr_stream.show() parts = curr_stream.getElementsByClass(m.stream.Part) voiceCount = len(parts) print ("voiceCount: ", str(voiceCount)) # change clefs of violas #curr_stream.getElementsByClass(m.stream.Part)[3].measure(1).clef = m.clef.TrebleClef() #curr_stream.getElementsByClass(m.stream.Part)[4].measure(1).clef = m.clef.TrebleClef() #curr_stream.getElementsByClass(m.stream.Part)[5].measure(1).clef = m.clef.TrebleClef() # change clefs of contrabas #curr_stream.getElementsByClass(m.stream.Part)[9].measure(1).clef = m.clef.BassClef() #curr_stream.show() s = m.stream.Score() rh = m.stream.Score() # .Part() only nest, Score can be used to chordify rh lh = m.stream.Score() # .Part() only nest, Score can be used to chordify lh #help(rh) # Violins violin1 = parts[0] violin2 = parts[1] violin3 = parts[2] # Violas viola1 = parts[3] viola2 = parts[4] viola3 = parts[5] # parts[x].measure(1) contains info about measure1 of part[x] # parts[3].measure(1).timeSignature # parts[3].measure(1).clef # Change Alto clefs of violas into TrebleClef #print ("before parts[3].measure(1).clef: "+ str(parts[3].measure(1).clef)) #parts[3].write(outputFormat, scorePath+"/alto."+outputFormat) viola1.measure(1).clef = m.clef.TrebleClef() viola2.measure(1).clef = m.clef.TrebleClef() viola3.measure(1).clef = m.clef.TrebleClef() # parts[3].write(outputFormat, scorePath+"/treble."+outputFormat) # check number of measures viola1 util 3 #measureCountViola1 = len (m.stream.iterator.RecursiveIterator(viola1, streamsOnly=True)) #print("Number of measures viola1: "+ str(measureCountViola1)) # check all clefs #for i in list(range(measureCountViola1)): # print("cleff("+str(i+1)+"): "+str(viola3.measure(i+1).clef) ) #measureCountViola2 = len (m.stream.iterator.RecursiveIterator(viola2, streamsOnly=True)) #print("Number of measures viola2: "+ str(measureCountViola2)) #measureCountViola3 = len (m.stream.iterator.RecursiveIterator(viola3, streamsOnly=True)) #print("Number of measures viola3: "+ str(measureCountViola3)) # The Violoncelli violoncello1 = parts[6] violoncello2 = parts[7] violoncello3 = parts[8] # The Contrabass contrabass1 = parts[9] #measureCountContrabass1 = len (m.stream.iterator.RecursiveIterator(contrabass1, streamsOnly=True)) #print("Number of measures contrabass1: "+ str(measureCountContrabass1)) # Change octava8 Bassclefs of contrabass into bassClef contrabass1.measure(1).clef = m.clef.BassClef() # Create right hand rh.append(violin1) rh.append(violin2) rh.append(violin3) rh.append(viola1) rh.append(viola2) rh.append(viola3) rh=rh.chordify() # reset PartName rh.partName = "" rh.partAbbreviation = "" # Why is this not set? rh.instrumentName = "Piano" rh.midiProgram="Piano" # Inputfile measure 12 and 13 ares differtent for violin1, violin2 an violin3 #rh.show() outputFile = scorePath + "/rh."+outputFormat #rh.write(outputFormat, outputFile) # Create left hand lh.append(violoncello1) lh.append(violoncello2) lh.append(violoncello3) lh.append(contrabass1) lh=lh.chordify() # reset PartName lh.partName = "" lh.partAbbreviation = "" # Why is this not set? lh.insert(m.instrument.Piano()) #lh.show() outputFile = scorePath + "/lh."+outputFormat #lh.write(outputFormat, outputFile) s.insert(0,rh) #print("type(s): "+str(type(s))) s.insert(0,lh) #print("type(s): "+str(type(s))) # See: http://web.mit.edu/music21/doc/moduleReference/moduleLayout.html#staffgroup staffGroup1 = m.layout.StaffGroup([rh, lh], name='Piano', abbreviation='Pno.', symbol='brace') s.insert(0, staffGroup1) outputFile = outputFileDFLT s.write(outputFormat, outputFile) s.insert(0, m.metadata.Metadata()) s.metadata.title = title s.metadata.composer = composer s.show() #print("type(s): "+str(type(s))) print("Program finished") 更改为on_text: root.SelectCut(self, text),然后将.py更改on_text: root.SelectCut()更改为def SelectCut(self, text):,然后更改文本,您可以使用def SelectCut(self):来阅读或更改微调框的文本,并将self.ids.category.text也添加到此功能