问题描述
过程:首先,您必须添加一个名称,然后程序将设置一个初学者(函数"umdie()"),然后屏幕"Wurfelbeginn"将向您显示玩家的姓名和他们所拥有的号码.之后,您单击按钮"Wer beginnt?"看看谁开始.
procedure: First you have to add a Name and than the Programm will set a Beginner (function "umdie()") after that the Screen "Wurfelbeginn" will Show you the names of the Players and the number which they had thorwn.After that you click on the Button "Wer beginnt?" to see who begins.
现在我的问题是:我想从弹出窗口中的Variablen类中显示变量"Beginner",但是如果我调用该变量,则什么都不会发生,但是控制台显示正确的名称.
And now my problem:i want to Showthe variable "Beginner" from the class Variablen in the popup, but if i call the variable nothing happens, but the console Shows the right names.
我做错了什么?
我的main.py代码:
my main.py Code:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition
from kivy.properties import ObjectProperty, ListProperty, StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.popup import Popup
from kivy.core.window import Window
from random import *
class Boxlayout(BoxLayout):
pass
class Textinput(TextInput):
pass
class Variablen():
Spielernamen = []
Beginner = 0
Zweiter = 0
def get_names(self, Namensabfrage):
Name1 = Namensabfrage.name_1.text
Name2 = Namensabfrage.name_2.text
self.Spielernamen.append(Name1)
self.Spielernamen.append(Name2)
print(Variablen.Spielernamen)
def umdie(self, Wurfelbeginn):
if Wurfelbeginn.Würfel1 > Wurfelbeginn.Würfel2:
self.Beginner = self.Spielernamen[0]
self.Zweiter = self.Spielernamen[1]
elif Wurfelbeginn.Würfel1 == Wurfelbeginn.Würfel2:
self.Beginner = self.Spielernamen[0]
self.Zweiter = Variablen.Spielernamen[1]
else:
self.Beginner = self.Spielernamen[1]
self.Zweiter = self.Spielernamen[0]
print(self.Beginner)
print(self.Zweiter)
class Namensabfrage(Screen):
name_1 = ObjectProperty()
name_2 = ObjectProperty()
def save_names(self):
vars = Variablen()
vars.get_names(self)
class Wurfelbeginn(Screen, Variablen):
Spieler1 = StringProperty("")
Spieler2 = StringProperty("")
Würfel1 = randint(1,6)
Würfel2 = randint(1,6)
WürfSp1 = str(Würfel1)
WürfSp2 = str(Würfel2)
def on_pre_enter(self):
self.Spieler1 = str(Variablen.Spielernamen[0] + " würfelt: ")
self.Spieler2 = str(Variablen.Spielernamen[1] + " würfelt: ")
Variablen.umdie(self, Wurfelbeginn)
def open_popup(self):
the_popup = CustomPopup()
the_popup.open()
class CustomPopup(Popup,Variablen):
Beginnertext = StringProperty("")
def on_pre_enter(self):
self.Beginnertext = str(Variablen.Beginner + "fängt an")
print("Hallo")
class Manager(ScreenManager):
namensabfrage = ObjectProperty(None)
wurfelbeginn = ObjectProperty(None)
class ScreensApp(App):
def build(self):
m = Manager(transition=SlideTransition())
return m
if __name__ == "__main__":
ScreensApp().run()
和这里的screes.kv:
and here screes.kv:
#:kivy 1.10.0
<Namensabfrage>:
name_1: name1
name_2: name2
Boxlayout:
orientation: 'vertical'
padding: 50
spacing: 50
Label:
text: "gebt hier eure Namen ein"
Textinput:
id: name1
size_hint: (1, .3)
text: ""
multiline: False
Textinput:
id: name2
size_hint: (1, .3)
text: ""
multiline: False
Button:
text: "Bestätigen"
size_hint:(1,.4)
on_press: root.save_names()
Button:
size_hint: (1, .5)
text: "Start"
on_press: root.manager.current = "Screen2"
<Wurfelbeginn>:
Boxlayout:
orientation: 'vertical'
spacing: 20
Button:
text: "Wer beginnt?"
size_hint:(1,.4)
font_size: 20
on_press: root.open_popup()
Label:
text: root.Spieler1
Label:
text: root.WürfSp1
font_size: 30
Label:
text: root.Spieler2
Label:
text: root.WürfSp2
font_size: 30
<CustomPopup>:
size_hint: .5, .5
auto_dismiss: False
title: "Beginner"
BoxLayout:
Label:
text: root.Beginnertext
Button:
text: "schließen"
on_press: root.dismiss()
<Manager>:
id: screen_manager
namensabfrage: Namensabfrage
wurfelbeginn: Wurfelbeginn
Namensabfrage:
id: Namensabfrage
name: "Screen1"
manager: screen_manager
Wurfelbeginn:
id: Wurfelbeginn
name: "Screen2"
manager: screen_manager
推荐答案
显示谁在弹出窗口中启动
有关详细信息,请参阅以下说明,示例和输出.
Display Who Starts in Popup Window
Please refer to the following explanations, example, and output for details.
将 root.save_names()替换为 app.root.variablen.get_names(root),并传递 root ,以便新创建的对象,变量可以被其他类访问.
Replace root.save_names() with app.root.variablen.get_names(root), and pass root so that the newly created object, Variablen is accessible by the other classes.
<Namensabfrage>:
...
Button:
text: "Bestätigen"
size_hint:(1,.4)
on_press: app.root.variablen.get_names(root)
类< Manager>
添加对自定义方法 on_pre_enter_callback 的调用,并传递新创建的对象 app.root.variablen ,以便其他类可以访问它.
class <Manager>
Add a call to customized method, on_pre_enter_callback and pass the newly created object, app.root.variablen so that it is accessible by the other classes.
<Manager>:
transition: SlideTransition()
...
Wurfelbeginn:
id: Wurfelbeginn
name: "Screen2"
manager: screen_manager
on_pre_enter: self.on_pre_enter_callback(app.root.variablen)
main.py
多重继承
不需要多重继承.从类中删除了变量".
main.py
Mulitple Inheritance
There is no need for multiple inheritance. Removed "Variablen" from classes.
class Wurfelbeginn(Screen):
...
class CustomPopup(Popup):
变量n
- 由于将字符串分配给属性/变量 Beginner 和 Zweiter ,因此将它们定义为字符串.
- 修复方法 umdie 中的拼写错误.将 self.Zweiter = Variablen.Spielernamen [1] 替换为 self.Zweiter = self.Spielernamen [1]
- 添加方法__init __
- Since string will be assigned to the attribute/variable Beginner and Zweiter, define them as string.
- Fix a typo error in method umdie. Replace self.Zweiter = Variablen.Spielernamen[1] with self.Zweiter = self.Spielernamen[1]
- Add a method __init__
摘要
class Variablen():
def __init__(self):
self.Spielernamen = []
self.Beginner = ''
self.Zweiter = ''
...
def umdie(self, Wurfelbeginn):
...
elif Wurfelbeginn.Würfel1 == Wurfelbeginn.Würfel2:
...
self.Zweiter = self.Spielernamen[1]
else:
...
类名称nsbfrage
- 已删除方法保存名称.
- 初始化为无的对象属性(名称_1,名称_2).
- Removed method save_names.
- Initialized ObjectProperties (name_1, name_2) to None.
摘要
class Namensabfrage(Screen):
name_1 = ObjectProperty(None)
name_2 = ObjectProperty(None)
Wurfelbeginn类
- 为新的类属性 variablen 添加 ObjectProperty
- 将方法 on_pre_enter 重命名为自定义方法 on_pre_enter_callback
- 将新的 ObjectProperty 连接到参数 variablen
- 将 Variablen 重命名为 variablen
- 将 self 传递给方法 umdie
- 将对象 self.variablen 传递给类 CustomPopup
- Add an ObjectProperty for new class attribute, variablen
- Rename method on_pre_enter to a customized method, on_pre_enter_callback
- Hook up the new ObjectProperty to the argument, variablen
- Renamed Variablen to variablen
- Pass self to method umdie
- Pass object, self.variablen to class CustomPopup
摘要
class Wurfelbeginn(Screen):
variablen = ObjectProperty(None)
...
def on_pre_enter_callback(self, variablen):
self.variablen = variablen
self.Spieler1 = str(variablen.Spielernamen[0] + " würfelt: ")
self.Spieler2 = str(variablen.Spielernamen[1] + " würfelt: ")
variablen.umdie(self)
def open_popup(self):
the_popup = CustomPopup(self.variablen)
CustomPopup类
为了显示谁是初学者,请添加方法 __ init __ 来填充类属性 Beginnertext .
class CustomPopup
In order to display who is the beginner, add a method __init__ to populate the class attribute, Beginnertext.
class CustomPopup(Popup):
Beginnertext = StringProperty('')
def __init__(self, variablen, **kwargs):
super(CustomPopup, self).__init__(**kwargs)
self.Beginnertext = str(variablen.Beginner + " fängt an")
print(self.Beginnertext)
print("Hallo")
班长
- 添加 ObjectProperty ,变量n
- 添加方法__init __
- 将ObjectProperty( variablen )连接到实例化对象 Variablen (Variablen类).
- Add an ObjectProperty, variablen
- Add a method __init__
- Hook up the ObjectProperty (variablen) to instantiated object, Variablen (class Variablen).
摘要
class Manager(ScreenManager):
variablen = ObjectProperty(None)
...
def __init__(self, **kwargs):
super(Manager, self).__init__(**kwargs)
self.variablen = Variablen() # Instantiate object Variablen
示例
main.py
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.popup import Popup
from random import *
class Boxlayout(BoxLayout):
pass
class Textinput(TextInput):
pass
class Variablen():
def __init__(self):
self.Spielernamen = []
self.Beginner = ''
self.Zweiter = ''
def get_names(self, Namensabfrage):
Name1 = Namensabfrage.name_1.text
Name2 = Namensabfrage.name_2.text
self.Spielernamen.append(Name1)
self.Spielernamen.append(Name2)
print(self.Spielernamen)
def umdie(self, Wurfelbeginn):
if Wurfelbeginn.Würfel1 > Wurfelbeginn.Würfel2:
self.Beginner = self.Spielernamen[0]
self.Zweiter = self.Spielernamen[1]
elif Wurfelbeginn.Würfel1 == Wurfelbeginn.Würfel2:
self.Beginner = self.Spielernamen[0]
self.Zweiter = self.Spielernamen[1]
else:
self.Beginner = self.Spielernamen[1]
self.Zweiter = self.Spielernamen[0]
print(self.Beginner)
print(self.Zweiter)
class Namensabfrage(Screen):
name_1 = ObjectProperty(None)
name_2 = ObjectProperty(None)
class Wurfelbeginn(Screen):
variablen = ObjectProperty(None)
Spieler1 = StringProperty('')
Spieler2 = StringProperty('')
Würfel1 = randint(1, 6)
Würfel2 = randint(1, 6)
WürfSp1 = str(Würfel1)
WürfSp2 = str(Würfel2)
def on_pre_enter_callback(self, variablen):
self.variablen = variablen
self.Spieler1 = str(variablen.Spielernamen[0] + " würfelt: ")
self.Spieler2 = str(variablen.Spielernamen[1] + " würfelt: ")
variablen.umdie(self)
def open_popup(self):
the_popup = CustomPopup(self.variablen)
the_popup.open()
class CustomPopup(Popup):
Beginnertext = StringProperty('')
def __init__(self, variablen, **kwargs):
super(CustomPopup, self).__init__(**kwargs)
self.Beginnertext = str(variablen.Beginner + " fängt an")
print(self.Beginnertext)
print("Hallo")
class Manager(ScreenManager):
variablen = ObjectProperty(None)
namensabfrage = ObjectProperty(None)
wurfelbeginn = ObjectProperty(None)
def __init__(self, **kwargs):
super(Manager, self).__init__(**kwargs)
self.variablen = Variablen() # Instantiate object Variablen
class ScreensApp(App):
def build(self):
return Manager()
if __name__ == "__main__":
ScreensApp().run()
screens.kv
#:kivy 1.10.0
#:import SlideTransition kivy.uix.screenmanager.SlideTransition
<Namensabfrage>:
name_1: name1
name_2: name2
Boxlayout:
orientation: 'vertical'
padding: 50
spacing: 50
Label:
text: "gebt hier eure Namen ein"
Textinput:
id: name1
size_hint: (1, .3)
text: ""
multiline: False
Textinput:
id: name2
size_hint: (1, .3)
text: ""
multiline: False
Button:
text: "Bestätigen"
size_hint:(1,.4)
on_press: app.root.variablen.get_names(root)
Button:
size_hint: (1, .5)
text: "Start"
on_press: root.manager.current = "Screen2"
<Wurfelbeginn>:
Boxlayout:
orientation: 'vertical'
spacing: 20
Button:
text: "Wer beginnt?"
size_hint:(1,.4)
font_size: 20
on_press: root.open_popup()
Label:
text: root.Spieler1
Label:
text: root.WürfSp1
font_size: 30
Label:
text: root.Spieler2
Label:
text: root.WürfSp2
font_size: 30
<CustomPopup>:
size_hint: .5, .5
auto_dismiss: False
title: "Beginner"
BoxLayout:
orientation: 'vertical'
Label:
text: root.Beginnertext
Button:
text: "schließen"
on_press: root.dismiss()
<Manager>:
id: screen_manager
transition: SlideTransition()
namensabfrage: Namensabfrage
wurfelbeginn: Wurfelbeginn
Namensabfrage:
id: Namensabfrage
name: "Screen1"
manager: screen_manager
Wurfelbeginn:
id: Wurfelbeginn
name: "Screen2"
manager: screen_manager
on_pre_enter: self.on_pre_enter_callback(app.root.variablen)
这篇关于Kivy:如何在弹出窗口中获取类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!