我正在做一个项目,在从kivy打开微调小部件时遇到了问题。我在网上找不到任何答案,所以我来这里:)
我尝试使用网格布局的浮动布局实例,它也不起作用。使用kivy==1.11.1 (在2.0.0rc1和1.11.0中都不起作用)
UI.py
import kivy
kivy.require('1.11.1')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.uix.button import Button
from kivy.core.window import Window
Window.clearcolor = (1, 1, 1, 1)
from tkinter import Tk
from tkinter.filedialog import askopenfilename
class CustRedButton(Button):
pass
class MainScreen(Screen):
def OpenData(self):
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
dir = askopenfilename(title='Open File', filetypes=(('CSV files', '*.csv'),))
if dir:
self.DataFileDir=dir
def toSettings(self):
try:
if self.DataFileDir:
pass
self.manager.current = 'settings'
except Exception as e:
if isinstance(e, AttributeError):
print('Got No Data')
self.manager.current = 'settings'
else:
print(e)
class SettingsScreen(Screen):
def __init__(self, **kwargs):
super(SettingsScreen, self).__init__(**kwargs)
self.spinner_values = ('House', 'Home', 'Car', 'Kivy')
def toMain(self):
try:
self.manager.current = 'main'
except Exception as e:
print(e)
def on_spinner_select(self, text):
print(text)
class ScreenManagment(ScreenManager):
main_screen= ObjectProperty(None)
settings_screen= ObjectProperty(None)
presentation= Builder.load_file('main.kv')
class MainApp(App):
def build(self):
return presentation
def runUI():
MainApp().run()
if __name__ == '__main__':
runUI()
和main.kv:
#: import NoTransition kivy.uix.screenmanager.NoTransition
<Button>:
font_size: root.width*0.1
size_hint: 0.2,0.17
color: 0,0,0,1
background_normal: ''
background_color: 0.8,0.8,0.8,1
<CustRedButton>:
font_size: root.width*0.19
size_hint: 0.1,0.1
color: 0,0,0,1
background_normal: ''
background_color: 1,0.1,0.1,1
<Label>:
color: 0,0,0,1
ScreenManagment:
id: screen_manager
main_screen: main_screen
settngs_screen: settings_screen
transition: NoTransition()
MainScreen:
name: 'main'
id: main_screen
manager: screen_manager
SettingsScreen:
name: 'settings'
id: settings_screen
manager: screen_manager
<MainScreen>:
BoxLayout:
orientation:'horizontal'
FloatLayout:
Button:
pos_hint: {'x':0.55,'top':0.6}
on_press: root.toSettings()
text: 'Go To Settings'
Button:
pos_hint: {'x':0.25,'top':0.6}
text: 'Open Data'
on_release: root.OpenData()
<SettingsScreen>:
BoxLayout:
orientation:'horizontal'
FloatLayout:
CustRedButton:
pos_hint:{'x':0, 'y':0}
on_press: root.toMain()
text: 'To Main'
GridLayout:
cols:2
pos_hint: {'x':-0.1, 'y':0.6}
size_hint: 1, 0.2
Label:
text:"Select"
Spinner:
pos_hint: {'center': (.7, .7)}
text: root.spinner_values[0]
values: root.spinner_values
on_text: root.on_spinner_select(text)
background_color: 0.5,0.5,0.5,1
所有其他窗口小部件都工作得很好,微调器从__初始化__中获取'spinner_values‘。我实际上不知道为什么我可以看到微调器,但不能打开它来选择值。
附注:我是StackOverflow新手,所以如果我没有输入足够的信息,请告诉我,我会添加它
发布于 2020-03-26 17:40:28
问题是您的<Button>:
规则应用于所有Buttons
,包括Spinner
中的Buttons
。将该规则更改为MyButton@Button>
并将其用于两个Buttons
,这样Spinner
就可以工作了。以下是您的kv
文件的修改版本,我认为它将执行您想要的操作:
#: import NoTransition kivy.uix.screenmanager.NoTransition
<MyButton@Button>:
font_size: root.width*0.1
size_hint: 0.2,0.17
color: 0,0,0,1
background_normal: ''
background_color: 0.8,0.8,0.8,1
<CustRedButton>:
font_size: root.width*0.19
size_hint: 0.1,0.1
color: 0,0,0,1
background_normal: ''
background_color: 1,0.1,0.1,1
<Label>:
color: 0,0,0,1
ScreenManagment:
id: screen_manager
main_screen: main_screen
settngs_screen: settings_screen
transition: NoTransition()
MainScreen:
name: 'main'
id: main_screen
manager: screen_manager
SettingsScreen:
name: 'settings'
id: settings_screen
manager: screen_manager
<MainScreen>:
BoxLayout:
orientation:'horizontal'
FloatLayout:
MyButton:
pos_hint: {'x':0.55,'top':0.6}
on_press: root.toSettings()
text: 'Go To Settings'
MyButton:
pos_hint: {'x':0.25,'top':0.6}
text: 'Open Data'
on_release: root.OpenData()
<SettingsScreen>:
BoxLayout:
orientation:'horizontal'
FloatLayout:
CustRedButton:
pos_hint:{'x':0, 'y':0}
on_press: root.toMain()
text: 'To Main'
GridLayout:
cols:2
pos_hint: {'x':-0.1, 'y':0.6}
size_hint: 1, 0.2
Label:
text:"Select"
Spinner:
pos_hint: {'center': (.7, .7)}
text: root.spinner_values[0]
values: root.spinner_values
on_text: root.on_spinner_select(text)
background_color: 0.5,0.5,0.5,1
https://stackoverflow.com/questions/60865253
复制