저는 Python과 함께 kivy를 사용하여 슬라이더가있는 빠른 앱을 개발하려고합니다. 슬라이더를 먼저 사용하여 설정을 결정한 다음 '제출'을 클릭 한 다음 원하는 이미지를 앱 창에로드해야합니다.
현재 .kv 파일에 버튼과 이미지를 개별적으로 삽입하는 예제가 있지만 연결 방법을 모르겠습니다.
BoxLayout:
orientation:'vertical'
size: root.width,root.height
font_size: 20
padding: 100
spacing: 10
Button:
text: 'press me'
on_press: print("ouch! More gently please")
on_release: print("ahhh")
on_state:
#print("my current state is {}".format(self.state))
size_hint: (0.3,0.3)
Image:
source: 'images\IMG_6980.jpg'
#allow_stretch: True
#keep_ratio: True
pos_hint: {'center_x':0.7}
on_press 문으로 무언가를해야 할 것 같지만 무엇을해야할지 잘 모르겠습니다. 도움을 주시면 감사하겠습니다.
설명한대로 on_press 메서드를 사용하여 버튼에서 이미지를 생성하는 방법에 대한 예제를 아래에 제공했습니다. 팩토리 모듈을 사용하여 * .kv 파일에서 생성 된 템플릿을 생성 할 수 있습니다. 따라서 프로그램을 완료하려면 이러한 템플릿을 더 많이 만든 다음 조건을 사용하여 on_press 메서드에서 적절한 이미지 템플릿을 생성합니다. 또는 Python 내에서 동적 템플릿을 만들 수 있지만, 제 예가 더 간단하다고 생각합니다.
test.py :
import kivy
kivy.require('2.0.0')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class TestBoxLayout(BoxLayout):
pass
class TestApp(App):
def build(self):
return TestBoxLayout()
if __name__ == '__main__':
TestApp().run()
test.kv :
#: import Factory kivy.factory.Factory
<TestImage@Image>:
source: 'test.jpg'
#allow_stretch: True
#keep_ratio: True
pos_hint: {'centre_X':0.7}
<TestBoxLayout>:
orientation:'vertical'
size: root.width,root.height
font_size: 20
padding: 100
spacing: 10
Button:
text: 'press me'
on_press: root.add_widget(Factory.TestImage())
on_release: print("ahhh")
on_state:
#print("my current state is {}".format(self.state))
size_hint: (0.3,0.3)
이 솔루션을 귀하의 의견으로 확장하기 위해 아래에 추가 예제를 제공했습니다. 필자의 예제는 이해하기 쉽고 확실히 더 효율적으로 수행 할 수 있도록 설계되었지만 이것이 문제에 대한 명확한 해결책이되기를 바랍니다. 이것이 귀하의 요구 사항을 충족하는 경우이를 베스트 답변으로 표시하십시오!
test.kv :
#: import Factory kivy.factory.Factory
<TestImage1@Image>:
source: 'test_1.jpg'
#allow_stretch: True
#keep_ratio: True
pos_hint: {'centre_X':0.7}
<TestImage2@Image>:
source: 'test_2.jpg'
#allow_stretch: True
#keep_ratio: True
pos_hint: {'centre_X':0.7}
<TestBoxLayout>:
orientation:'vertical'
size: root.width,root.height
font_size: 20
padding: 100
spacing: 10
Slider:
id: slider
min: 1
max: 2
step: 1
Button:
text: 'press me'
on_press:
if slider.value == 1: root.add_widget(Factory.TestImage1())
elif slider.value == 2: root.add_widget(Factory.TestImage2())
on_release: print("ahhh")
on_state:
#print("my current state is {}".format(self.state))
size_hint: (0.3,0.3)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다