我正在尝试将popup.dismiss()
函数绑定到Kivy弹出窗口中BoxLayout
内的按钮。
我不断收到错误说:
无类型'对象没有属性' bind'。
我一直在寻找2天的答案,似乎无法找到我要找的东西。任何帮助将不胜感激。
此外,如果您对我的代码有任何提示,我将不胜感激,因为我是新手,我确信有很多错误。
我还没有添加.kv代码,因为我认为它不适用于我目前的问题。
代码段:
import kivy
kivy.require('1.9.1')
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from KivyCalendar import CalendarWidget
from KivyCalendar import DatePicker
from kivy.properties import ObjectProperty, OptionProperty
from kivy.uix.listview import ListItemButton
from kivy.uix.modalview import ModalView
from kivy.uix.popup import Popup
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
import sqlite3
import datetime
class LogSheet(TabbedPanel):
def __init__(self, **kwargs):
super(LogSheet, self).__init__(**kwargs)
#self.register_event_type('popup_1')
sq = sqlite3
wd = Widget
md = ModalView
lb = Label
op = OptionProperty
bt = Button
def popup_1(self):
box = BoxLayout(orientation = 'vertical', padding = (10))
box.add_widget(Label(text = "Are you sure you want to save data? \n Once saved cannot re-edit!!!!"))
btn1 = box.add_widget(Button(text = "YES TO SAVE"))
btn2 = box.add_widget(Button(text = "NO TO GO BACK"))
btn1.bind(on_press = popup.dismiss)
btn2.bind(on_press = hypochlorinator_1)
popup = Popup(title='Check if Correct', title_size= (30),
title_align = 'center', content = box,
size_hint=(None, None), size=(400, 400), auto_dismiss = True)
popup.open()
def hypochlorinator_1(self):
dd1 = self.date_data.text
vd1 = self.volt_data.text
ad1 = self.amp_data.text
ld1 = self.load_data.text
conn = sqlite3.connect('logsheet.db')
c = conn.cursor()
def create_table():
c.execute('''CREATE TABLE IF NOT EXISTS logSheets(id INTEGER
PRIMARY KEY,
date DATETIME, volts REAL, amps REAL, setpoint REAL)''')
def data_entry():
c.execute('''INSERT INTO logSheets (date, volts, amps, setpoint)
VALUES (?, ?, ?, ?)''', (dd1, vd1, ad1, ld1))
conn.commit()
create_table()
data_entry()
c.close()
conn.close()
class LogSheetApp(App):
def build(self):
return LogSheet()
lsApp = LogSheetApp()
lsApp.run()
错误讯息:
File "C:\Users\Kids\Documents\Visual Studio 2015\Projects\KivyTuts2\logsheet.kv", line 294, in <module>
on_press: root.popup_1()
File "C:\Users\Kids\Documents\Visual Studio 2015\Projects\KivyTuts2\logsheet.py", line 87, in popup_1
btn1.bind(popup.dismiss)
AttributeError: 'NoneType' object has no attribute 'bind'
答案 0 :(得分:2)
你有:
btn1 = box.add_widget(Button(text = "YES TO SAVE"))
btn2 = box.add_widget(Button(text = "NO TO GO BACK"))
btn1
和btn2
是add_widget
方法的回报,也就是说,它们是None
。您必须先实例化该按钮,然后使用add_widget方法:
btn1 = Button(text = "YES TO SAVE")
btn2 = Button(text = "NO TO GO BACK")
box.add_widget(btn1)
box.add_widget(btn2)
现在,btn1
和btn2
是Button的实例,他们有bind
方法。
您还可以将on_press
参数传递给构造函数:
box.add_widget(Button(text = "YES TO SAVE", on_press=popup.dismiss))
box.add_widget(Button(text = "NO TO GO BACK", on_press=self.hypochlorinator_1))
你的功能应该是:
def popup_1(self):
box = BoxLayout(orientation = 'vertical', padding = (10))
box.add_widget(Label(text = "Are you sure you want to save data? \n Once saved cannot re-edit!!!!"))
btn1 = Button(text = "YES TO SAVE")
btn2 = Button(text = "NO TO GO BACK")
box.add_widget(btn1)
box.add_widget(btn2)
popup = Popup(title='Check if Correct', title_size= (30),
title_align = 'center', content = box,
size_hint=(None, None), size=(400, 400),
auto_dismiss = True)
btn1.bind(on_press = popup.dismiss)
btn2.bind(on_press = self.hypochlorinator_1)
popup.open()
或:
def popup_1(self):
box = BoxLayout(orientation = 'vertical', padding = (10))
box.add_widget(Label(text = "Are you sure you want to save data? \n Once saved cannot re-edit!!!!"))
popup = Popup(title='Check if Correct', title_size= (30),
title_align = 'center', content = box,
size_hint=(None, None), size=(400, 400),
auto_dismiss = True)
box.add_widget(Button(text = "YES TO SAVE", on_press=popup.dismiss))
box.add_widget(Button(text = "NO TO GO BACK", on_press=self.hypochlorinator_1))
popup.open()
<强>输出:强>
编辑: hypochlorinator_1
应该收到额外的参数:
def hypochlorinator_1(self, instance):