我是Python的初学者和Kivy,我想构建一个具有多个接口的接口。
我决定从一个非常简单的应用程序开始,其中包括违反音阶。
第一个界面(类AskUser)有2个按钮:scale(class Scale)和tonality(class Tonality)。
在界面比例中,用户定义比例的间隔(7个音符的比例),在音调界面中,他给出了音调。
我成功编程了每个界面,但我没有成功 通过单击第一个界面上的相应按钮来启动界面比例和音调。 (见下面的代码)
有没有人有一个简单的解决方案来解决这个问题? 提前谢谢。
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.popup import Popup
user_interface=Builder.load_string('''
BoxLayout:
orientation: 'vertical'
Label:
size_hint_y: 0.2
font_size: 20
GridLayout:
padding: 50
spacing: 10
cols: 3
rows: 3
Button:
text: 'Scale'
Button:
text: 'Tonality'
''')
class AskUser(App):
def build(self):
return user_interface
AskUser().run()
class Scale(BoxLayout):
def build(self):
self.orientation='vertical'
self.spacing=20
self.haut()
self.chiffres()
self.BoutonValider=Button(text='Valider',size_hint=(0.5,0.15),pos_hint={'right': 0.75},background_color=[0,0,1])
self.BoutonValider.bind(on_press=self.valider)
self.add_widget(self.BoutonValider)
def haut(self):
Layout1=GridLayout(cols=2,size_hint_y=0.2,padding=20)
scale='Scale=?'
self.scale=Label(text=scale,font_size=30, color=[1,1,1,1])
Layout1.add_widget(self.scale)
self.reponse=''
self.user_reponse = TextInput(text=self.reponse,font_size=30)
Layout1.add_widget(self.user_reponse)
self.add_widget(Layout1)
def chiffres(self):
Layout2=GridLayout(cols=2,size_hint_y=0.5,spacing=10)
self.Bouton1=Button(text='1')
self.Bouton1.bind(on_press=self.nombre)
Layout2.add_widget(self.Bouton1)
self.Bouton2=Button(text='2')
self.Bouton2.bind(on_press=self.nombre)
Layout2.add_widget(self.Bouton2)
self.Bouton3=Button(text='3')
self.Bouton3.bind(on_press=self.nombre)
Layout2.add_widget(self.Bouton3)
self.Bouton4=Button(text='4')
self.Bouton4.bind(on_press=self.nombre)
Layout2.add_widget(self.Bouton4)
self.add_widget(Layout2)
def nombre(self,instance):
self.reponse+=str(int((instance.text)))
self.user_reponse.text=(self.reponse)
def valider(self,instance):
reponse=((self.user_reponse.text))
sum_scale = sum([int(i) for i in str(reponse)])
if sum_scale ==12 and len(reponse)==7:
content = Button(text='Scale ok !', font_size=50)
popup = Popup(title='',content=content, size_hint=(0.5,0.5))
content.bind(on_press=popup.dismiss)
popup.open()
else :
content = Button(text='Not a 12 tones scale !', font_size=50)
popup = Popup(title='',content=content, size_hint=(0.5,0.5))
content.bind(on_press=popup.dismiss)
self.reponse=''
popup.open()
class Tonality(BoxLayout):
def build(self):
self.orientation='vertical'
self.spacing=20
self.haut()
self.chiffres()
self.BoutonValider=Button(text='Valider',size_hint=(0.5,0.15),pos_hint={'right': 0.75},background_color=[0,0,1])
self.BoutonValider.bind(on_press=self.valider)
self.add_widget(self.BoutonValider)
def haut(self):
Layout1=GridLayout(cols=2,size_hint_y=0.2,padding=20)
scale='Tonality=?'
self.scale=Label(text=scale,font_size=30, color=[1,1,1,1])
Layout1.add_widget(self.scale)
self.reponse=''
self.user_reponse = TextInput(text=self.reponse,font_size=30)
Layout1.add_widget(self.user_reponse)
self.add_widget(Layout1)
def chiffres(self):
Layout2=GridLayout(cols=2,size_hint_y=0.5,spacing=10)
self.Bouton1=Button(text='c')
self.Bouton1.bind(on_press=self.nombre)
Layout2.add_widget(self.Bouton1)
self.Bouton2=Button(text='c#')
self.Bouton2.bind(on_press=self.nombre)
Layout2.add_widget(self.Bouton2)
self.Bouton3=Button(text='d')
self.Bouton3.bind(on_press=self.nombre)
Layout2.add_widget(self.Bouton3)
self.Bouton4=Button(text='d#')
self.Bouton4.bind(on_press=self.nombre)
Layout2.add_widget(self.Bouton4)
self.add_widget(Layout2)
def nombre(self,instance):
self.reponse=instance.text
self.user_reponse.text=(self.reponse)
def valider(self,instance):
reponse=((self.user_reponse.text))
if len(reponse)<3:
content = Button(text='Tonality ok !', font_size=50)
popup = Popup(title='',content=content, size_hint=(0.5,0.5))
content.bind(on_press=popup.dismiss)
popup.open()
class scaleApp(App):
def build(self):
Panel1=Scale()
Panel1.build()
Panel2=Tonality()
Panel2.build()
return Panel1
if __name__ == '__main__':
scaleApp().run()
答案 0 :(得分:0)
解决方案:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.popup import Popup
Builder.load_string('''
<user_interface>:
orientation: 'vertical'
Label:
size_hint_y: 0.2
font_size: 20
GridLayout:
padding: 50
spacing: 10
cols: 3
rows: 3
Button:
text: 'Scale'
Button:
text: 'Tonality'
''')
class user_interface(BoxLayout):
pass
class AskUser(App):
def build(self):
return user_interface()
AskUser().run()