Python Kivy:创建一个可以访问设置面板和 运行 命令的 FloatLayout
Python Kivy: Create a FloatLayout with access to Setting panel and running command
我目前正在尝试学习使用 Kivy GUI 语言。
我正在尝试创建一个带有不同小部件(按钮、文件浏览器...)的布局,其中一个按钮访问自定义设置面板以设置一些参数,然后一个按钮使用这些按钮 运行 命令参数.
我无法使命令按钮正常工作,这可能是一个简单的面向对象问题,但我看不出哪里出了问题。
任何帮助将不胜感激。
另外,如果有人知道如何使用命令按钮 (btn) 访问设置中存储的某些值,那就太好了。
到目前为止,这是我的代码:
# main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.settings import SettingsWithSidebar
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from settingsjson import settings_json
Builder.load_string('''
<Interface>:
Button:
text: 'Analysis Settings'
pos_hint: {"x": 0.2, 'y': 0.3}
on_release: app.open_settings()
size_hint: 0.2, 0.2
''')
class Interface(FloatLayout): #
pass
class SettingsApp(App):
def build(self):
#Color setting#
blue = (0, 0, 1.5, 2.5)
self.settings_cls = SettingsWithSidebar
self.use_kivy_settings = False
setting = self.config.get('example', 'boolexample')
btn = Button(text='Run!',
background_color=blue,
size_hint=(.2, .2),
pos_hint={'x':.5, 'y':.3})
btn.bind(on_press=self.command1)
Interface.add_widget(Interface.btn)
return Interface()
def command1(self, event):
print("button touched")
def build_config(self, config):
config.setdefaults('example', {
'boolexample': True,
'numericexample': 10,
'optionexample': 'Analysis type1',
'stringexample': 'PO12345'})
def build_settings(self, settings):
settings.add_json_panel('Parameter of Analysis',
self.config,
data=settings_json)
def on_config_change(self, config, section,
key, value):
print config, section, key, value
SettingsApp().run()
settings.ini
[example]
boolexample = 1
optionexample = Analysis type1
stringexample = 46464, hdfhf, jhdgfjhf, hjdgfjhf
numericexample = 455
optionsexample = Analysis type2
settingsjson.py
import json
settings_json = json.dumps([
{'type': 'title',
'title': 'Analysis parameters'},
{'type': 'bool',
'title': 'Add accessin number list?',
'desc': 'Choose if you want to add an Accession list to your analysis',
'section': 'example',
'key': 'boolexample'},
{'type': 'numeric',
'title': 'Select maximum number of protei to display',
'desc': 'This will determine maximum protein displayed on graph',
'section': 'example',
'key': 'numericexample'},
{'type': 'options',
'title': 'Selection your Analysis option',
'desc': 'Choose for an analysis option',
'section': 'example',
'key': 'optionsexample',
'options': ['Analysis type1', 'Analysis type2', 'Analysis type3']},
{'type': 'string',
'title': 'Type specific accession number to display',
'desc': 'Type some accession numbers to display',
'section': 'example',
'key': 'stringexample'}])
I'm having trouble to get the command button working and it is
probably a simple object oriented problem but I can't see what's wrong.
您写道:
Interface.add_widget(Interface.btn)
看看您的界面 class--没有任何地方定义 btn
属性。但是,在该行之前,您构建了一个名为 btn
的小部件,因此您应该这样写:
Interface.add_widget(btn)
这会导致另一个错误:
TypeError: unbound method add_widget() must be called with Interface
instance as first argument (got Button instance instead)
使用 class name
调用方法与使用 class 的 an instance
调用方法不同:
class Dog:
def bark(self):
print('woof')
d = Dog()
d.bark()
Dog.bark()
--output:--
woof
Traceback (most recent call last):
File "main.py", line 9, in <module>
Dog.bark()
TypeError: unbound method bark() must be called with
Dog instance as first argument (got nothing instead) #<****HEY!
当您使用 Dog class 的实例调用 bark() 时,python 自动将该实例作为第一个参数传递给该方法,该方法被分配给变量 self
.当您使用 class.
调用方法时,不会发生这种情况
要修复该错误,您需要更改行:
Interface.add_widget(btn)
至:
i = Interface()
i.add_widget(btn)
或者,更曲折地:
class Dog:
def __init__(self):
print(self)
def bark(self):
print(self)
print('woof')
d = Dog()
Dog.bark(d) #<*** Manually pass an instance.
--output:--
woof
Also if anyone have an idea how to use the command button ( btn) to
access some of the values stored from the settings it would be great.
应用 class 有一个 config property,可让您访问所有设置:
class SettingsApp(App):
...
...
def onpress_button(self, button):
print "button touched"
config = self.config
print config.getint("example", "numericexample")
print config.getfloat("example", "numericexample")
print config.getboolean("example", "boolexample")
print config.get("example", "stringexample")
print config.getdefault("example", "non-existent", "hello")
print config.items('example')
--output:--
button touched
10
10.0
True
PO12345
hello
[('boolexample', 'True'), ('optionexample', 'Analysis type1'), ('stringexample', 'PO12345'), ('numericexample', '10')]
查看 python 对象的 ConfigParser as well as the kivy docs for the Config 文档以查看所有可用的方法。
我目前正在尝试学习使用 Kivy GUI 语言。 我正在尝试创建一个带有不同小部件(按钮、文件浏览器...)的布局,其中一个按钮访问自定义设置面板以设置一些参数,然后一个按钮使用这些按钮 运行 命令参数.
我无法使命令按钮正常工作,这可能是一个简单的面向对象问题,但我看不出哪里出了问题。 任何帮助将不胜感激。 另外,如果有人知道如何使用命令按钮 (btn) 访问设置中存储的某些值,那就太好了。
到目前为止,这是我的代码:
# main.pyfrom kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.settings import SettingsWithSidebar
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from settingsjson import settings_json
Builder.load_string('''
<Interface>:
Button:
text: 'Analysis Settings'
pos_hint: {"x": 0.2, 'y': 0.3}
on_release: app.open_settings()
size_hint: 0.2, 0.2
''')
class Interface(FloatLayout): #
pass
class SettingsApp(App):
def build(self):
#Color setting#
blue = (0, 0, 1.5, 2.5)
self.settings_cls = SettingsWithSidebar
self.use_kivy_settings = False
setting = self.config.get('example', 'boolexample')
btn = Button(text='Run!',
background_color=blue,
size_hint=(.2, .2),
pos_hint={'x':.5, 'y':.3})
btn.bind(on_press=self.command1)
Interface.add_widget(Interface.btn)
return Interface()
def command1(self, event):
print("button touched")
def build_config(self, config):
config.setdefaults('example', {
'boolexample': True,
'numericexample': 10,
'optionexample': 'Analysis type1',
'stringexample': 'PO12345'})
def build_settings(self, settings):
settings.add_json_panel('Parameter of Analysis',
self.config,
data=settings_json)
def on_config_change(self, config, section,
key, value):
print config, section, key, value
SettingsApp().run()
settings.ini
[example]
boolexample = 1
optionexample = Analysis type1
stringexample = 46464, hdfhf, jhdgfjhf, hjdgfjhf
numericexample = 455
optionsexample = Analysis type2
settingsjson.py
import json
settings_json = json.dumps([
{'type': 'title',
'title': 'Analysis parameters'},
{'type': 'bool',
'title': 'Add accessin number list?',
'desc': 'Choose if you want to add an Accession list to your analysis',
'section': 'example',
'key': 'boolexample'},
{'type': 'numeric',
'title': 'Select maximum number of protei to display',
'desc': 'This will determine maximum protein displayed on graph',
'section': 'example',
'key': 'numericexample'},
{'type': 'options',
'title': 'Selection your Analysis option',
'desc': 'Choose for an analysis option',
'section': 'example',
'key': 'optionsexample',
'options': ['Analysis type1', 'Analysis type2', 'Analysis type3']},
{'type': 'string',
'title': 'Type specific accession number to display',
'desc': 'Type some accession numbers to display',
'section': 'example',
'key': 'stringexample'}])
I'm having trouble to get the command button working and it is probably a simple object oriented problem but I can't see what's wrong.
您写道:
Interface.add_widget(Interface.btn)
看看您的界面 class--没有任何地方定义 btn
属性。但是,在该行之前,您构建了一个名为 btn
的小部件,因此您应该这样写:
Interface.add_widget(btn)
这会导致另一个错误:
TypeError: unbound method add_widget() must be called with Interface instance as first argument (got Button instance instead)
使用 class name
调用方法与使用 class 的 an instance
调用方法不同:
class Dog:
def bark(self):
print('woof')
d = Dog()
d.bark()
Dog.bark()
--output:--
woof
Traceback (most recent call last):
File "main.py", line 9, in <module>
Dog.bark()
TypeError: unbound method bark() must be called with
Dog instance as first argument (got nothing instead) #<****HEY!
当您使用 Dog class 的实例调用 bark() 时,python 自动将该实例作为第一个参数传递给该方法,该方法被分配给变量 self
.当您使用 class.
要修复该错误,您需要更改行:
Interface.add_widget(btn)
至:
i = Interface()
i.add_widget(btn)
或者,更曲折地:
class Dog:
def __init__(self):
print(self)
def bark(self):
print(self)
print('woof')
d = Dog()
Dog.bark(d) #<*** Manually pass an instance.
--output:--
woof
Also if anyone have an idea how to use the command button ( btn) to access some of the values stored from the settings it would be great.
应用 class 有一个 config property,可让您访问所有设置:
class SettingsApp(App):
...
...
def onpress_button(self, button):
print "button touched"
config = self.config
print config.getint("example", "numericexample")
print config.getfloat("example", "numericexample")
print config.getboolean("example", "boolexample")
print config.get("example", "stringexample")
print config.getdefault("example", "non-existent", "hello")
print config.items('example')
--output:--
button touched
10
10.0
True
PO12345
hello
[('boolexample', 'True'), ('optionexample', 'Analysis type1'), ('stringexample', 'PO12345'), ('numericexample', '10')]
查看 python 对象的 ConfigParser as well as the kivy docs for the Config 文档以查看所有可用的方法。