問題描述
我的第一個屏幕上有一個 TextInput,我想在第二個屏幕的標(biāo)簽中使用接收到的文本.我怎樣才能做到這一點(diǎn)?由于可以有不同的玩家,我創(chuàng)建了一個類 Players ,它為每個玩家存儲一個名字和他/她的分?jǐn)?shù).在第二個屏幕中,我還嘗試創(chuàng)建一個可以編輯點(diǎn)(標(biāo)簽文本)的按鈕,但是當(dāng)我單擊它時,什么也沒有發(fā)生.(我也是新手.)
I have a TextInput on my first screen and I want to use the received text in a Label on my second screen. How can I do this? Since there can be different players, I created a class Players which stores for every player a name and his/her points. In the second screen, I also tried to create a button which can edit the points (text of Label), but when I click it, nothing happens. (I am also new to classes.)
在下面的代碼中,我標(biāo)記了相關(guān)的行.
In the code below, I marked the relevant lines.
所以概述:
第一個屏幕:使用 textInput 提取用戶名 + 使用 Player 類創(chuàng)建播放器實(shí)例
First screen: extract user name(s) with textInput + create player instances with Player class
第二個屏幕:在標(biāo)簽中使用玩家名稱 + 在標(biāo)簽中使用玩家點(diǎn) + 創(chuàng)建 2 個按鈕,從這個點(diǎn)"標(biāo)簽中添加/減去點(diǎn)
Second screen: Use player name in a Label + use player points in Label + create 2 Buttons that add/subtract points from this 'point' Label
我知道這里有類似的情況,但它對我的 .py 文件沒有幫助:如何在 Kivy/Python 的另一個屏幕中引用 TextInput?
I know there is a similar case here, but it doesn't help me for my .py file: How to ref a TextInput from one screen in another screen in Kivy/Python?
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.properties import ObjectProperty, NumericProperty
class Player:
def __init__(self, name):
self.name = name
self.points = 0
def reset_points(self):
self.points = 0
def add_point(self, *args):
self.points += 1
def subtract_point(self, *args):
self.points -= 1
class WelcomeWindow(Screen):
# Introduce names of the 4 players
def __init__(self, **kwargs):
super(WelcomeWindow, self).__init__(**kwargs)
self.name = "welcomewindow"
self.layout = "layout_welcome_window"
global_layout = GridLayout(rows=3)
self.add_widget(global_layout)
label_player_i = Label(text="Name Player ")
global_layout.add_widget(label_player_i)
name_input_player_i = TextInput(id="player ", text="player", multiline=False) # <--- user inputs name here
global_layout.add_widget(name_input_player_i)
self.player1 = Player(name_input_player_i.text) # <--- name is assigned to player here
# Create button to go to next screen
go_further_button = Button(text="Go to first round")
go_further_button.bind(on_release=self.go_further)
global_layout.add_widget(go_further_button)
def go_further(self, *args):
self.manager.current = "firstround"
self.manager.transition.direction = "left"
class FirstRound(Screen):
#Give explanation of first round + option to add points for every player
def __init__(self, **kwargs):
super(FirstRound, self).__init__(**kwargs)
self.name = "firstround"
self.layout = "layout_first_round"
#Create layout
global_layout = GridLayout(rows=4)
self.add_widget(global_layout)
#Create Labels
label_player_name_i = Label(text=WelcomeWindow().player1.name) # <--- Label should get the name of the player here
global_layout.add_widget(label_player_name_i)
label_player_points_i = Label(text=str(WelcomeWindow().player1.points)) # <--- Label should get points of player
global_layout.add_widget((label_player_points_i))
#Create Buttons
button_minus = Button(text="-", font_size=100, id="minus_button")
button_minus.bind(on_release=WelcomeWindow().player1.subtract_point) # <--- When button pushed: should subtract point
global_layout.add_widget(button_minus)
button_plus = Button(text="+", font_size=100, id="plus_button")
button_plus.bind(on_release=WelcomeWindow().player1.add_point) # <--- When button pushed: should add point
global_layout.add_widget(button_plus)
WindowManager = ScreenManager()
WindowManager.add_widget(WelcomeWindow())
WindowManager.add_widget(FirstRound())
class KingenApp(App):
def build(self):
return WindowManager
if __name__ == "__main__":
KingenApp().run()
推薦答案
你的代碼有幾個問題:
- 當(dāng)您創(chuàng)建該
Widget
的實(shí)例時,將調(diào)用任何Widget
的__init__()
方法.所以FirstRound
的__init__()
方法在WindowManager.add_widget(FirstRound())
行被調(diào)用.那個時候,WelcomeWindow
的TextInput
是不能輸入文字的,因此您無法獲得當(dāng)時的玩家名稱. Player
實(shí)例的創(chuàng)建 (self.player1 = Player(name_input_player_i.text)
) 在用戶創(chuàng)建Player
實(shí)例之前有機(jī)會輸入玩家姓名.- 在
FirstRound
的__init__()
方法中使用WelcomeWindow()
會創(chuàng)建一個新的WelcomeWindow
與您的 GUI 中的那個無關(guān).所以從那個實(shí)例中提取的任何信息都是沒有用的. - 在您的
FirstRound
中,玩家點(diǎn)標(biāo)簽在創(chuàng)建Label
時從Player
類實(shí)例獲取其數(shù)據(jù).之后更改Player
的points
屬性將不會影響Label
.
- The
__init__()
method of anyWidget
is called when you create an instance of thatWidget
. So the__init__()
method ofFirstRound
is called at the lineWindowManager.add_widget(FirstRound())
. At that time no text can have been entered into theTextInput
ofWelcomeWindow
, so you cannot get the player name at that time. - The creation of the
Player
instance (self.player1 = Player(name_input_player_i.text)
) creates thePlayer
instance before the user has a chance to enter a player name. - The use of
WelcomeWindow()
in the__init__()
method ofFirstRound
creates a new instance ofWelcomeWindow
that is unrelated to the one in your GUI. So any info extracted from that instance is of no use. - In your
FirstRound
, the player points label gets its data from thePlayer
class instance at the time that theLabel
is created. Changing the thepoints
attribute ofPlayer
after that will have no effect on theLabel
.
第一個問題可以通過將大部分代碼移出 FirstRound
的 __init__()
方法,并將其放在 on_enter()
顯示該 Screen
時運(yùn)行的方法.
The first issue can be handled by moving most of your code out of the __init__()
method of FirstRound
, and place it in an on_enter()
method that is run when that Screen
is displayed.
第二個問題可以通過將 Player
實(shí)例的創(chuàng)建移到 go_further()
方法中來處理,因?yàn)樗鼤陔x開 WelcomeWindow<時執(zhí)行/代碼>.
The second issue can be handled by moving the creation of the Player
instance into the go_further()
method, since it gets executed when leaving the WelcomeWindow
.
第三個問題可以通過將 WelcomeWindow()
的那些用法替換為 self.manager.get_screen('welcomewindow')
來處理 的實(shí)際實(shí)例>WelcomeWindow
在您的 GUI 中.
The third issue can be handled by replacing those uses of WelcomeWindow()
with self.manager.get_screen('welcomewindow')
to access the actual instance of WelcomeWindow
that is in your GUI.
這是您的代碼的修改版本,它可以完成這三件事:
Here is a modified version of your code that does those three things:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
class Player:
def __init__(self, name):
self.name = name
self.points = 0
def reset_points(self):
self.points = 0
def add_point(self, *args):
self.points += 1
def subtract_point(self, *args):
self.points -= 1
class WelcomeWindow(Screen):
# Introduce names of the 4 players
def __init__(self, **kwargs):
super(WelcomeWindow, self).__init__(**kwargs)
self.name = "welcomewindow"
self.layout = "layout_welcome_window"
global_layout = GridLayout(rows=3)
self.add_widget(global_layout)
label_player_i = Label(text="Name Player ")
global_layout.add_widget(label_player_i)
self.name_input_player_i = TextInput(id="player ", text="player", multiline=False) # <--- user inputs name here
global_layout.add_widget(self.name_input_player_i)
# Create button to go to next screen
go_further_button = Button(text="Go to first round")
go_further_button.bind(on_release=self.go_further)
global_layout.add_widget(go_further_button)
def go_further(self, *args):
self.player1 = Player(self.name_input_player_i.text) # <--- name is assigned to player here
self.manager.current = "firstround"
self.manager.transition.direction = "left"
class FirstRound(Screen):
#Give explanation of first round + option to add points for every player
def __init__(self, **kwargs):
super(FirstRound, self).__init__(**kwargs)
self.name = "firstround"
self.layout = "layout_first_round"
def on_enter(self, *args):
#Create layout
global_layout = GridLayout(rows=4)
self.add_widget(global_layout)
#Create Labels
welcome_window = self.manager.get_screen('welcomewindow') # get a reference to the WelcomeWindow instance
label_player_name_i = Label(text=welcome_window.player1.name) # <--- Label should get the name of the player here
global_layout.add_widget(label_player_name_i)
label_player_points_i = Label(text=str(welcome_window.player1.points)) # <--- Label should get points of player
global_layout.add_widget((label_player_points_i))
#Create Buttons
button_minus = Button(text="-", font_size=100, id="minus_button")
button_minus.bind(on_release=welcome_window.player1.subtract_point) # <--- When button pushed: should subtract point
global_layout.add_widget(button_minus)
button_plus = Button(text="+", font_size=100, id="plus_button")
button_plus.bind(on_release=welcome_window.player1.add_point) # <--- When button pushed: should add point
global_layout.add_widget(button_plus)
WindowManager = ScreenManager()
WindowManager.add_widget(WelcomeWindow())
WindowManager.add_widget(FirstRound())
class KingenApp(App):
def build(self):
return WindowManager
if __name__ == "__main__":
KingenApp().run()
第四個問題值得自己提出一個問題,但可能涉及使用 kivy 語言.
The fourth issue deserves a question of its own, but likely involves using kivy language.
這篇關(guān)于Kivy - 在 .py 文件中的另一個屏幕中使用來自一個屏幕的 TextInput 的文本的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!