【发布时间】:2018-05-22 07:35:23
【问题描述】:
所以我有两个文件:
- returnStation.py
- returnStationLayout.kv
我想要实现的目标:有两个屏幕。一种是数字键盘。输入号码并按 Enter 后,它会将您带到下一个屏幕。我希望另一个屏幕显示你刚刚键入的数字。
我面临的问题:我尝试访问我尝试更改的标签的 ID 以显示数字,但它不起作用:/我在终端中没有收到任何错误。
我会以错误的方式访问这些值吗?如果是这样,请告知如何最好地在两个屏幕中进行。感谢任何帮助!
这是文件 - returnStation.py:
我尝试更改标签的方法是通过 getPoints()
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
pass
class PhoneGridLayout(GridLayout):
def backspace(self, textString):
newTextString = textString[0:-1]
self.display.text = newTextString
def getPoints(self, phoneNumber):
st = ScreenTwo()
st.ids.memberStatus.text = phoneNumber #THIS IS HOW I ATTEMPTED TO CHANGE THE LABEL
class ReturnStationLayoutApp(App):
pass
mainscreen = ScreenOne()
mainlayout = PhoneGridLayout()
mainscreen.add_widget(mainlayout)
if __name__ == '__main__':
ReturnStationLayoutApp().run()
这是文件 - returnStationLayout.kv:
我要更改的标签一直在这个文件的底部
ScreenManager:
id: screen_manager
ScreenOne:
id: screen_one
name: 'menu'
manager: 'screen_manager'
ScreenTwo:
id: screen_two
name: 'settings'
manager: 'screen_manager'
<CustButton@Button>:
font_size: 32
<ScreenOne>:
PhoneGridLayout:
id: numberPad
display: entry
rows: 5
padding: [300,200]
spacing: 10
# Where input is displayed
BoxLayout:
Label:
text: "+65"
font_size: 50
size_hint: 0.2, 1
TextInput:
id: entry
font_size: 50
multiline: False
padding: [20, ( self.height - self.line_height ) / 2]
BoxLayout:
spacing: 10
CustButton:
text: "1"
on_press: entry.text += self.text
CustButton:
text: "2"
on_press: entry.text += self.text
CustButton:
text: "3"
on_press: entry.text += self.text
CustButton:
text: "DEL"
on_press: numberPad.backspace(entry.text)
BoxLayout:
spacing: 10
CustButton:
text: "4"
on_press: entry.text += self.text
CustButton:
text: "5"
on_press: entry.text += self.text
CustButton:
text: "6"
on_press: entry.text += self.text
CustButton:
text: "AC"
on_press: entry.text = ""
BoxLayout:
spacing: 10
CustButton:
text: "7"
on_press: entry.text += self.text
CustButton:
text: "8"
on_press: entry.text += self.text
CustButton:
text: "9"
on_press: entry.text += self.text
CustButton:
text: "Enter" #HERE IS THE ENTER BUTTON
on_press:
app.root.transition.direction = 'left'
app.root.transition.duration = 1
app.root.current = 'settings'
numberPad.getPoints(entry.text)
BoxLayout:
spacing: 10
Label:
text: ""
CustButton:
text: "0"
on_press: entry.text += self.text
Label:
text: ""
Label:
text: ""
<ScreenTwo>:
BoxLayout:
Label:
id: memberStatus
text: '' #THIS IS THE LABEL I AM TRYING TO CHANGE
Button:
text: 'Back to menu'
on_press:
app.root.transition.direction = "right"
app.root.current = 'menu'
【问题讨论】:
标签: python python-2.7 kivy