我正在尝试将 kivy 弹出窗口中的按钮绑定到 popup.dismiss 函数并不断收到错误消息
I am trying to bind a button in a kivy popup to the popup.dismiss function and keep getting an error
我正在尝试将 popup.dismiss()
函数绑定到 Kivy 弹出窗口中 BoxLayout
内的按钮。
我一直收到一条错误消息:
None Type' object has no attribute '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'
你有:
btn1 = box.add_widget(Button(text = "YES TO SAVE"))
btn2 = box.add_widget(Button(text = "NO TO GO BACK"))
btn1
和btn2
是add_widget
方法的return,即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):
我正在尝试将 popup.dismiss()
函数绑定到 Kivy 弹出窗口中 BoxLayout
内的按钮。
我一直收到一条错误消息:
None Type' object has no attribute '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'
你有:
btn1 = box.add_widget(Button(text = "YES TO SAVE"))
btn2 = box.add_widget(Button(text = "NO TO GO BACK"))
btn1
和btn2
是add_widget
方法的return,即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):