• <i id='pvrrk'><tr id='pvrrk'><dt id='pvrrk'><q id='pvrrk'><span id='pvrrk'><b id='pvrrk'><form id='pvrrk'><ins id='pvrrk'></ins><ul id='pvrrk'></ul><sub id='pvrrk'></sub></form><legend id='pvrrk'></legend><bdo id='pvrrk'><pre id='pvrrk'><center id='pvrrk'></center></pre></bdo></b><th id='pvrrk'></th></span></q></dt></tr></i><div id='pvrrk'><tfoot id='pvrrk'></tfoot><dl id='pvrrk'><fieldset id='pvrrk'></fieldset></dl></div>

  • <legend id='pvrrk'><style id='pvrrk'><dir id='pvrrk'><q id='pvrrk'></q></dir></style></legend>
      <bdo id='pvrrk'></bdo><ul id='pvrrk'></ul>

        <tfoot id='pvrrk'></tfoot>

        <small id='pvrrk'></small><noframes id='pvrrk'>

        Kivy - 在 .py 文件中的另一个屏幕中使用来自一个

        时间:2023-10-10
        <tfoot id='CstQc'></tfoot>
          <i id='CstQc'><tr id='CstQc'><dt id='CstQc'><q id='CstQc'><span id='CstQc'><b id='CstQc'><form id='CstQc'><ins id='CstQc'></ins><ul id='CstQc'></ul><sub id='CstQc'></sub></form><legend id='CstQc'></legend><bdo id='CstQc'><pre id='CstQc'><center id='CstQc'></center></pre></bdo></b><th id='CstQc'></th></span></q></dt></tr></i><div id='CstQc'><tfoot id='CstQc'></tfoot><dl id='CstQc'><fieldset id='CstQc'></fieldset></dl></div>

              • <small id='CstQc'></small><noframes id='CstQc'>

                <legend id='CstQc'><style id='CstQc'><dir id='CstQc'><q id='CstQc'></q></dir></style></legend>

                  <tbody id='CstQc'></tbody>

                  <bdo id='CstQc'></bdo><ul id='CstQc'></ul>
                • 本文介绍了Kivy - 在 .py 文件中的另一个屏幕中使用来自一个屏幕的 TextInput 的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我的第一个屏幕上有一个 TextInput,我想在第二个屏幕的标签中使用接收到的文本.我怎样才能做到这一点?由于可以有不同的玩家,我创建了一个类 Players ,它为每个玩家存储一个名字和他/她的分数.在第二个屏幕中,我还尝试创建一个可以编辑点(标签文本)的按钮,但是当我单击它时,什么也没有发生.(我也是新手.)

                  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.)

                  在下面的代码中,我标记了相关的行.

                  In the code below, I marked the relevant lines.

                  所以概述:

                  • 第一个屏幕:使用 textInput 提取用户名 + 使用 Player 类创建播放器实例

                  • First screen: extract user name(s) with textInput + create player instances with Player class

                  第二个屏幕:在标签中使用玩家名称 + 在标签中使用玩家点 + 创建 2 个按钮,从这个点"标签中添加/减去点

                  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()
                  

                  推荐答案

                  你的代码有几个问题:

                  • 当您创建该 Widget 的实例时,将调用任何 Widget__init__() 方法.所以 FirstRound__init__() 方法在 WindowManager.add_widget(FirstRound()) 行被调用.那个时候,WelcomeWindowTextInput是不能输入文字的,因此您无法获得当时的玩家名称.
                  • Player 实例的创建 (self.player1 = Player(name_input_player_i.text)) 在用户创建 Player 实例之前有机会输入玩家姓名.
                  • FirstRound__init__()方法中使用WelcomeWindow()会创建一个新的WelcomeWindow 与您的 GUI 中的那个无关.所以从那个实例中提取的任何信息都是没有用的.
                  • 在您的 FirstRound 中,玩家点标签在创建 Label 时从 Player 类实例获取其数据.之后更改 Playerpoints 属性将不会影响 Label.
                  • The __init__() method of any Widget is called when you create an instance of that Widget. So the __init__() method of FirstRound is called at the line WindowManager.add_widget(FirstRound()). At that time no text can have been entered into the TextInput of WelcomeWindow , 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 the Player instance before the user has a chance to enter a player name.
                  • The use of WelcomeWindow() in the __init__() method of FirstRound creates a new instance of WelcomeWindow 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 the Player class instance at the time that the Label is created. Changing the the points attribute of Player after that will have no effect on the Label.

                  第一个问题可以通过将大部分代码移出 FirstRound__init__() 方法,并将其放在 on_enter() 显示该 Screen 时运行的方法.

                  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 实例的创建移到 go_further() 方法中来处理,因为它会在离开 WelcomeWindow<时执行/代码>.

                  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') 来处理 的实际实例>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.

                  这篇关于Kivy - 在 .py 文件中的另一个屏幕中使用来自一个屏幕的 TextInput 的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                    <tbody id='q8q0C'></tbody>
                  • <bdo id='q8q0C'></bdo><ul id='q8q0C'></ul>
                  • <i id='q8q0C'><tr id='q8q0C'><dt id='q8q0C'><q id='q8q0C'><span id='q8q0C'><b id='q8q0C'><form id='q8q0C'><ins id='q8q0C'></ins><ul id='q8q0C'></ul><sub id='q8q0C'></sub></form><legend id='q8q0C'></legend><bdo id='q8q0C'><pre id='q8q0C'><center id='q8q0C'></center></pre></bdo></b><th id='q8q0C'></th></span></q></dt></tr></i><div id='q8q0C'><tfoot id='q8q0C'></tfoot><dl id='q8q0C'><fieldset id='q8q0C'></fieldset></dl></div>
                        • <tfoot id='q8q0C'></tfoot>

                            <legend id='q8q0C'><style id='q8q0C'><dir id='q8q0C'><q id='q8q0C'></q></dir></style></legend>

                            <small id='q8q0C'></small><noframes id='q8q0C'>