每当我选择微调器值时如何隐藏标签?
Posted
技术标签:
【中文标题】每当我选择微调器值时如何隐藏标签?【英文标题】:How to hide the label whenever I select the spinner value? 【发布时间】:2020-07-16 12:16:21 【问题描述】:实际上每当我点击微调器值(官方)标签时 创建,当时我点击另一个微调器值“正常” 创建的标签被隐藏。这种情况工作正常,但来到我的 第二种情况——每当我首先点击微调器值(正常)时 时间,显示错误
进口kivy kivy.require('1.10.0') 从 kivy.core.window 导入窗口 从 kivy.app 导入应用程序 从 kivy.uix.floatlayout 导入 FloatLayout 从 kivy.uix.label 导入标签 从 kivy.graphics 导入颜色、矩形 从 kivy.uix.textinput 导入 TextInput 从 kivy.uix.spinner 导入微调器
Window.size = (500, 400) Window.clearcolor = (0.1, 0.1, 0.3, 0.2)
def show_selected_value(微调器,文本): print('选定进程', spinner, '有文本', text)
类 LRefConfigAutomation(App): 配置=无
def build_config(self, config):
config.setdefaults('LefConfigWindowSection',
'PartInputLabel': 'Please Enter your Part No',
'TypeOfProcessLabel': 'Select Type Of Process',
'NormalFolderName': 'Enter Normal Folder Name',
)
self.config = config
def build(self):
config = self.config
root = FloatLayout()
lbl = config.get('LefConfigWindowSection', 'PartInputLabel')
self.label01 = Label(text=lbl, pos_hint="center_x": .66, "center_y": .90, size_hint=(1.0, 1.0), halign="left",
valign="middle", font_name='Georgia')
self.label01.bind(size=self.label01.setter('text_size'))
self.label01.font_size = '14.5dp' # something that'll give texture bigger than phone's screen size
root.add_widget(self.label01)
with self.label01.canvas:
Color(0, 0, 0, 0)
Rectangle(pos=self.label01.pos, size=self.label01.size)
self.txtKemNo = TextInput(pos_hint="center_x": .66, "center_y": .90, size_hint=(None, None),
font_name="Georgia", size=(100, 30), multiline=False, hint_text="Part Number")
root.add_widget(self.txtKemNo)
lbl = config.get('LefConfigWindowSection', 'TypeOfProcessLabel')
self.label02 = Label(text=lbl, pos_hint="center_x": .66, "center_y": .75, size_hint=(1.0, 1.0), halign="left",
valign="middle", font_name='Georgia')
self.label02.bind(size=self.label02.setter('text_size'))
self.label02.font_size = '14.5dp' # something that'll give texture bigger than phone's screen size
root.add_widget(self.label02)
with self.label02.canvas:
Color(0, 0, 0, 0)
Rectangle(pos=self.label02.pos, size=self.label02.size)
self.requestsspinner = Spinner(
# default value shown
text='Select Process',
# available values will be binded to the combo
values=("Normal", "Official"),
# just for positioning in our example
size_hint=(None, None),
size=(110, 20),
pos_hint='center_x': .66, 'center_y': .75, font_name='Georgia')
self.requestsspinner.font_size = '14.5dp'
self.requestsspinner.bind(text=show_selected_value)
self.requestsspinner.bind(text=self.CreateLable)
root.add_widget(self.requestsspinner)
return root
def CreateLable(self, spinner, text):
config = self.config
if text == "Official":
lbl = config.get('LefConfigWindowSection', 'NormalFolderName')
self.label03 = Label(text=lbl, pos_hint="center_x": .66, "center_y": .65, size_hint=(1.0, 1.0),
halign="left", valign="middle", font_name='Georgia')
self.label03.bind(size=self.label03.setter('text_size'))
self.label03.font_size = '14.5dp'
self.root.add_widget(self.label03)
with self.label03.canvas:
Color(0, 0, 0, 0)
Rectangle(pos=self.label03.pos, size=self.label03.size)
elif text == "Normal":
self.label03.text = ""
如果 name == "main": LRefConfigAutomation().run()
【问题讨论】:
你能添加你的错误吗? 我添加了错误图片 能否请您也制作一个minimal, reproducible Example,然后将您的代码格式化为代码? 实际上我无法做到这一点,我遇到了很多错误,请你帮帮我 github.com/BollamReddy-Python-Vba/python/blob/master/TestData --- 我已将代码粘贴到 GitHub 中,请查看 【参考方案1】:在 CreateLabel 中,您需要将创建部分移到 if 条件之外: 所以这部分
lbl = config.get('LefConfigWindowSection', 'NormalFolderName')
self.label03 = Label(text=lbl, pos_hint="center_x": .66, "center_y": .65, size_hint=(1.0, 1.0),
halign="left", valign="middle", font_name='Georgia')
self.label03.bind(size=self.label03.setter('text_size'))
self.label03.font_size = '14.5dp'
self.root.add_widget(self.label03)
应该在if text == "Official":
之前。
否则,您会收到错误消息,因为您尚未创建 self.label03
。
完成 CreateLabel:
def CreateLable(self, spinner, text):
config = self.config
lbl = config.get('LefConfigWindowSection', 'NormalFolderName')
self.label03 = Label(text=lbl, pos_hint="center_x": .66, "center_y": .65, size_hint=(1.0, 1.0),
halign="left", valign="middle")
self.label03.bind(size=self.label03.setter('text_size'))
self.label03.font_size = '14.5dp'
self.root.add_widget(self.label03)
if text == "Official":
with self.label03.canvas:
Color(0, 0, 0, 0)
Rectangle(pos=self.label03.pos, size=self.label03.size)
elif text == "Normal":
self.label03.text = ""
CreateLabel 中还有一个问题:
self.label03 = Label(...)
每次调用 CreateLabel 时,都会为 self.label03 分配一个新标签,而不是第一个。所以不是隐藏第一个,而是创建一个新的并隐藏它。有多种解决方法,但这里有一种:
def build(self):
... # add these two lines to create the Label once
self.CreateLabel()
root.add_widget(self.label03)
# Use another function (UpdateLabel) to update the value
self.requestsspinner.bind(text=show_selected_value)
self.requestsspinner.bind(text=self.UpdateLable)
...
# Only called once to create the Label
def CreateLabel(self):
self.label03 = Label(text="", pos_hint="center_x": .66, "center_y": .65, size_hint=(1.0, 1.0),
halign="left", valign="middle")
self.label03.bind(size=self.label03.setter('text_size'))
self.label03.font_size = '14.5dp'
with self.label03.canvas:
Color(0, 0, 0, 0)
Rectangle(pos=self.label03.pos, size=self.label03.size)
# Changes the Label
def UpdateLable(self, spinner, text):
if text == "Official":
config = self.config
self.label03.text = config.get('LefConfigWindowSection', 'NormalFolderName')
elif text == "Normal":
self.label03.text = ""
【讨论】:
它不工作,如果你有任何其他解决方案,你能给我任何其他解决方案 实际上每当我点击微调器值时,正常标签应该隐藏以上是关于每当我选择微调器值时如何隐藏标签?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Android 中为微调器捕获 onClick 事件