正如我在标题中提到的,我正在创建一个带有视频和两个按钮的弹出窗口 - 现在我想实现这些按钮的功能 - 所以,我创建了一个方法,当弹出一个按钮时调用我创建了 - 但是这总是返回TypeError:addme()只需要1个参数(给定2个) - 如果我将此方法设为静态则返回AttributeError:' Button'对象没有属性' on_text'
from __future__ import print_function
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.popup import Popup
from kivy.properties import StringProperty
from kivy.properties import ObjectProperty
from kivy.uix.progressbar import ProgressBar
from kivy.uix.video import Video
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import WidgetException
from functools import partial
kv = """
<MainPage>:
manager:screen_mgr
do_default_tab: False
textinputtext: waybill.text
ScreenManager:
id: screen_mgr
#transition: FadeTransition
Screen:
manager: screen_mgr
id: intro
orientation: 'horizontal'
name: 'User'
canvas.before:
Rectangle:
pos: self.pos
size: self.size
AnchorLayout:
anchor_x: 'center'
anchor_y: 'center'
BoxLayout:
orientation:'horizontal'
size_hint: .5, .1
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
spacing: 20
pos_hint: {'center_x':.8, 'center_y': .8}
AnchorLayout:
anchor_x: 'left'
size_hint_x: .5
TextInput:
id: waybill
width: 20
text: root.textinputtext
multiline: False
height: self.minimum_height
size_hint_y: None
font_size: 30
focus: True
on_text: self.text
on_text_validate:app.on_waybill()
AnchorLayout:
anchor_x: 'right'
size_hint_x: None
Button:
size_hint: None, None
height: 50
width: self.texture_size[0]
padding: 10, 10
text: "Compare"
on_release: root.compareClicked()
AnchorLayout:
anchor_x: 'right'
anchor_y: 'bottom'
BoxLayout:
orientation:'horizontal'
size_hint: .5, .1
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
spacing: 50
pos_hint: {'center_x':.8, 'center_y': .8}
AnchorLayout:
anchor_x: 'right'
size_hint_x: None
Button:
id: statebtn
size_hint: None, None
height: 50
width: self.texture_size[0]
padding: 10, 10
text: "State"
AnchorLayout:
anchor_x: 'left'
anchor_y: 'top'
BoxLayout:
orientation:'horizontal'
size_hint: .5, .1
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
spacing: 50
pos_hint: {'center_x':.5, 'center_y': .5}
AnchorLayout:
anchor_x: 'right'
size_hint_x: None
Button:
size_hint: None, None
height: 50
width: self.texture_size[0]
padding: 10, 10
on_release:root.current = root.switch_screen()
text: "Admin"
TabbedPanelHeader:
text: intro.name
# store a screen name to link the tab to a screen
screen: intro.name
"""
Builder.load_string(kv)
waybill = TextInput(text="Enter Waybill No.", multiline=False)
class MainPage(TabbedPanel):
screen_mgr = ObjectProperty(None)
textinputtext = StringProperty()
def __init__(self, **kwargs):
super(MainPage, self).__init__(**kwargs)
self.register_event_type('on_text')
self.textinputtext = "Enter Waybill No."
def on_text(self):
return self.textinputtext
def on_text_validate(self):
return self.textinputtext
def on_focus(self, obj, focused):
if not focused:
self.dispatch('on_text_validate')
def compareClicked(self):
self.progress_bar = ProgressBar()
self.progress_bar.value = 1
print (self.on_text())
if self.on_text()!='Enter Waybill No.' :
self.popup_1()
def popup_1(self):
self.box = BoxLayout(orientation='vertical', padding=(10))
popup = Popup(title='Warning: Please Place', title_size=(30),
title_align='center', content=self.box,
size_hint=(None, None), size=(400, 400),
auto_dismiss=False)
self.box.add_widget(Button(text="YES TO COMPARE", on_release=partial(self.addme)))
self.box.add_widget(Button(text="NO TO GO BACK", on_release=popup.dismiss))
popup.bind()
popup.open()
#@staticmethod
def addme(self):
print (self.on_text())
def switch_screen(self):
self.manager.transition = FadeTransition()
self.manager.current = self.manager.previous()
return self.manager.current
def switch_to(self, header):
self.manager.current = header.screen
self.current_tab.state = "normal"
header.state = 'down'
self._current_tab = header
class KartScanApp(App):
def build(self):
return MainPage()
def on_waybill(self):
waybill.bind(text=MainPage.on_text)
return waybill.text
if __name__ == '__main__':
KartScanApp().run()
请帮我解决这个问题,我在这里遗漏了一些基础知识
答案 0 :(得分:0)
我知道我的代码有什么问题,每当我从弹出窗口中的按钮的on_release事件调用任何方法时,该方法总是将按钮作为对象 - 所以当你创建一个方法接受一个可以保存按钮对象的参数。以下是解决方案:
from __future__ import print_function
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.popup import Popup
from kivy.properties import StringProperty
from kivy.properties import ObjectProperty
from kivy.uix.progressbar import ProgressBar
from kivy.uix.video import Video
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import WidgetException
from functools import partial
kv = """
<MainPage>:
manager:screen_mgr
do_default_tab: False
textinputtext: waybill.text
ScreenManager:
id: screen_mgr
#transition: FadeTransition
Screen:
manager: screen_mgr
id: intro
orientation: 'horizontal'
name: 'User'
canvas.before:
Rectangle:
pos: self.pos
size: self.size
AnchorLayout:
anchor_x: 'center'
anchor_y: 'center'
BoxLayout:
orientation:'horizontal'
size_hint: .5, .1
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
spacing: 20
pos_hint: {'center_x':.8, 'center_y': .8}
AnchorLayout:
anchor_x: 'left'
size_hint_x: .5
TextInput:
id: waybill
width: 20
text: root.textinputtext
multiline: False
height: self.minimum_height
size_hint_y: None
font_size: 30
focus: True
on_text: self.text
on_text_validate:app.on_waybill()
AnchorLayout:
anchor_x: 'right'
size_hint_x: None
Button:
size_hint: None, None
height: 50
width: self.texture_size[0]
padding: 10, 10
text: "Compare"
on_release: root.compareClicked()
AnchorLayout:
anchor_x: 'right'
anchor_y: 'bottom'
BoxLayout:
orientation:'horizontal'
size_hint: .5, .1
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
spacing: 50
pos_hint: {'center_x':.8, 'center_y': .8}
AnchorLayout:
anchor_x: 'right'
size_hint_x: None
Button:
id: statebtn
size_hint: None, None
height: 50
width: self.texture_size[0]
padding: 10, 10
text: "State"
AnchorLayout:
anchor_x: 'left'
anchor_y: 'top'
BoxLayout:
orientation:'horizontal'
size_hint: .5, .1
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
spacing: 50
pos_hint: {'center_x':.5, 'center_y': .5}
AnchorLayout:
anchor_x: 'right'
size_hint_x: None
Button:
size_hint: None, None
height: 50
width: self.texture_size[0]
padding: 10, 10
on_release:root.current = root.switch_screen()
text: "Admin"
TabbedPanelHeader:
text: intro.name
# store a screen name to link the tab to a screen
screen: intro.name
"""
Builder.load_string(kv)
waybill = TextInput(text="Enter Waybill No.", multiline=False)
class MainPage(TabbedPanel):
screen_mgr = ObjectProperty(None)
textinputtext = StringProperty()
def __init__(self, **kwargs):
super(MainPage, self).__init__(**kwargs)
self.register_event_type('on_text')
self.textinputtext = "Enter Waybill No."
def on_text(self):
return self.textinputtext
def on_text_validate(self):
return self.textinputtext
def on_focus(self, obj, focused):
if not focused:
self.dispatch('on_text_validate')
def compareClicked(self):
self.progress_bar = ProgressBar()
self.progress_bar.value = 1
print (self.on_text())
if self.on_text()!='Enter Waybill No.' :
self.popup_1()
def popup_1(self):
self.box = BoxLayout(orientation='vertical', padding=(10))
popup = Popup(title='Warning: Please Place', title_size=(30),
title_align='center', content=self.box,
size_hint=(None, None), size=(400, 400),
auto_dismiss=False)
self.box.add_widget(Button(text="YES TO COMPARE", on_release=partial(self.addme)))
self.box.add_widget(Button(text="NO TO GO BACK", on_release=popup.dismiss))
popup.bind()
popup.open()
#@staticmethod
def addme(self, button):
print (self.on_text())
def switch_screen(self):
self.manager.transition = FadeTransition()
self.manager.current = self.manager.previous()
return self.manager.current
def switch_to(self, header):
self.manager.current = header.screen
self.current_tab.state = "normal"
header.state = 'down'
self._current_tab = header
class KartScanApp(App):
def build(self):
return MainPage()
def on_waybill(self):
waybill.bind(text=MainPage.on_text)
return waybill.text
if __name__ == '__main__':
KartScanApp().run()