如何在kivy的另一个屏幕中更改标签文本值
所以我正在使用 Kivy/KivyMD 制作在线井字游戏,我有点卡在这里试图编辑另一个屏幕的标签值。
这是 main.py
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
from requests import get
ip = get('https://api.ipify.org').text
class OptionScreen(Screen):
pass
class JoinServer(Screen):
pass
class CreateServer(Screen):
pass
class WindowManager(ScreenManager):
pass
class TicTacToeApp(MDApp):
def __init__(self, **kwargs):
self.title = "TicTacToe Online"
super().__init__(**kwargs)
def build(self):
TicTacToeApp.build.kv = Builder.load_file('styles\main.kv')
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Gray"
return TicTacToeApp.build.kv
def join(self):
TicTacToeApp.build.kv.current = 'join'
def create(self):
TicTacToeApp.build.kv.current = 'create'
if __name__ == "__main__":
TicTacToeApp().run()
这是 main.kv
文件:
#:kivy 2.0.0
WindowManager:
OptionScreen:
name: 'option'
JoinServer:
name: 'join'
CreateServer:
name: 'create'
<OptionScreen>:
MDCard:
size_hint: None, None
size: 700, 500
pos_hint: {"center_x": 0.5, "center_y": 0.5}
elevation: 10
spacing: 25
padding: 25
orientation: 'vertical'
MDLabel:
text: "Choose an option"
font_size: "28"
padding_y: 15
size_hint_y: None
halign: 'center'
pos_hint: {'center_x': 0.5,'center_y':0.5}
MDRoundFlatButton:
text:"Join Server"
font_size: 20
pos_hint: {'center_x':0.5}
on_press: app.join()
MDRoundFlatButton:
text:"Create Server"
font_size: 20
pos_hint: {'center_x':0.5}
on_press: app.create()
Widget:
size_hint_y: None
height: 90
<JoinServer>:
MDCard:
size_hint: None, None
size: 700, 500
pos_hint: {"center_x": 0.5, "center_y": 0.5}
elevation: 10
spacing: 25
padding: 25
orientation: 'vertical'
MDLabel:
text: "Join Server"
font_size: "28"
padding_y: 15
size_hint_y: None
halign: 'center'
pos_hint: {'center_x': 0.5,'center_y':0.5}
Widget:
size_hint_y: None
height: 325
<CreateServer>:
MDCard:
size_hint: None, None
size: 700, 500
pos_hint: {"center_x": 0.5, "center_y": 0.5}
elevation: 10
spacing: 25
padding: 25
orientation: 'vertical'
MDLabel:
text: "Create Server"
font_size: 40
padding_y: 15
size_hint_y: None
halign: 'center'
pos_hint: {'center_x': 0.5,'center_y':0.5}
MDLabel:
text: "Server Address: "
id: address
font_size: 18
size_hint_y: None
halign: 'center'
pos_hint: {'center_x': 0.5,'center_y':0.5}
MDTextField:
mode: 'round'
id: passw
hint_text: "Password"
size_hint_x: None
width: 150
font_size: 18
pos_hint: {'center_x': 0.5}
MDRoundFlatButton:
text:"Create Server"
font_size: 20
pos_hint: {'center_x': 0.5}
on_press: app.create_s()
Widget:
size_hint_y: None
height: 20
我想更改 id address
的标签文本当我切换到 CreateServer
屏幕时,CreateServer
屏幕会立即切换到其他内容。
我还想知道如何在 kivy 中切换屏幕后立即执行代码。
So I was making an online tic tac toe game using Kivy/KivyMD and im kinda stuck here trying to edit the value of a label of another screen.
here is the main.py
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.uix.screenmanager import ScreenManager, Screen
from requests import get
ip = get('https://api.ipify.org').text
class OptionScreen(Screen):
pass
class JoinServer(Screen):
pass
class CreateServer(Screen):
pass
class WindowManager(ScreenManager):
pass
class TicTacToeApp(MDApp):
def __init__(self, **kwargs):
self.title = "TicTacToe Online"
super().__init__(**kwargs)
def build(self):
TicTacToeApp.build.kv = Builder.load_file('styles\main.kv')
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "Gray"
return TicTacToeApp.build.kv
def join(self):
TicTacToeApp.build.kv.current = 'join'
def create(self):
TicTacToeApp.build.kv.current = 'create'
if __name__ == "__main__":
TicTacToeApp().run()
This is the main.kv
file:
#:kivy 2.0.0
WindowManager:
OptionScreen:
name: 'option'
JoinServer:
name: 'join'
CreateServer:
name: 'create'
<OptionScreen>:
MDCard:
size_hint: None, None
size: 700, 500
pos_hint: {"center_x": 0.5, "center_y": 0.5}
elevation: 10
spacing: 25
padding: 25
orientation: 'vertical'
MDLabel:
text: "Choose an option"
font_size: "28"
padding_y: 15
size_hint_y: None
halign: 'center'
pos_hint: {'center_x': 0.5,'center_y':0.5}
MDRoundFlatButton:
text:"Join Server"
font_size: 20
pos_hint: {'center_x':0.5}
on_press: app.join()
MDRoundFlatButton:
text:"Create Server"
font_size: 20
pos_hint: {'center_x':0.5}
on_press: app.create()
Widget:
size_hint_y: None
height: 90
<JoinServer>:
MDCard:
size_hint: None, None
size: 700, 500
pos_hint: {"center_x": 0.5, "center_y": 0.5}
elevation: 10
spacing: 25
padding: 25
orientation: 'vertical'
MDLabel:
text: "Join Server"
font_size: "28"
padding_y: 15
size_hint_y: None
halign: 'center'
pos_hint: {'center_x': 0.5,'center_y':0.5}
Widget:
size_hint_y: None
height: 325
<CreateServer>:
MDCard:
size_hint: None, None
size: 700, 500
pos_hint: {"center_x": 0.5, "center_y": 0.5}
elevation: 10
spacing: 25
padding: 25
orientation: 'vertical'
MDLabel:
text: "Create Server"
font_size: 40
padding_y: 15
size_hint_y: None
halign: 'center'
pos_hint: {'center_x': 0.5,'center_y':0.5}
MDLabel:
text: "Server Address: "
id: address
font_size: 18
size_hint_y: None
halign: 'center'
pos_hint: {'center_x': 0.5,'center_y':0.5}
MDTextField:
mode: 'round'
id: passw
hint_text: "Password"
size_hint_x: None
width: 150
font_size: 18
pos_hint: {'center_x': 0.5}
MDRoundFlatButton:
text:"Create Server"
font_size: 20
pos_hint: {'center_x': 0.5}
on_press: app.create_s()
Widget:
size_hint_y: None
height: 20
I want to change the text of the label with the id address
in the CreateServer
screen to something else as soon as i switch to the CreateServer
screen.
I was also wondering how to carry out a code as soon as you switch screens in kivy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要在
CreateServer
屏幕中更改带有 idaddress
的标签,只需转到CreateServer
类并进行以下更改。并在切换屏幕时执行代码。
使用上面答案中使用的
on_enter
函数。一旦屏幕变为活动状态,就会触发on_enter
函数。我希望它回答了你的问题。
To change the label with the id
address
in theCreateServer
screen simply go theCreateServer
class and do the following changes.and to carry out a code when u switch screens.
use the
on_enter
function which was used in the answer above. theon_enter
function is triggered as soon as the screen becomes active.I hope it answered your question.