2
我在stackoverflow上發現了一個類似的主題,但不幸的是它沒有幫助我。Kivy Python中的下拉菜單
這是我第一次嘗試認真編程GUI,我真的很生氣。 我一次只做一步,走向我最終需要的一步。
現在我試圖在我的小部件的左上角添加一個簡單的下拉菜單,當它被選中時,它的元素應該調用一個函數。我真的在kivy文檔中找到了這個,在這個論壇上,但我無法真正解決這個問題。
import multiprocessing
from mesh import MeshApp
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
import os
MAINDIR = os.path.dirname(os.path.realpath(__file__))
categories = {}
def getCategories():
for dir in os.walk(MAINDIR):
if len(dir[1]) == 0:
filelist = set()
for mesh in dir[2]:
filelist.add(mesh.replace('_FRONT.png','').replace('_SIDE.png','').replace('_TOP.png',''))
categories.update({dir[0]: filelist})
#class CategoriesList(DropDown):
# pass
class MainWindow(Widget):
def __init__(self):
#self.categorieslist = CategoriesList()
categories_list = DropDown()
for i in categories.keys():
btn = Button(text=i.replace(MAINDIR, ''), size_hint_y=None, height=30)
btn.bind(on_release=lambda btn: categories_list.select(btn.text))
categories_list.add_widget(btn)
mainbutton = Button(text='Choose directory', size_hint=(1, 1))
mainbutton.bind(on_release=categories_list.open)
categories_list.bind(on_select=lambda instance, x: setattr(mainbutton, 'text', x))
#and now???
class RenderApp(App):
def build(self):
self.launchMeshApp()
return MainWindow()
def launchMeshApp(self):
app = MeshApp()
p = multiprocessing.Process(target=app.run)
p.start()
if __name__ == '__main__':
getCategories()
RenderApp().run()
和:
#:kivy 1.9.1
<MainWindow>:
canvas.before:
Color:
rgba: 0.6, 0.6, 1, 1
Rectangle:
pos: self.pos
size: self.size
canvas:
Color:
rgba: 0, 0, 0, 0.5
Rectangle:
pos: 0, self.height * 5/6 - 1
size: self.width, 2
我創建了下拉列表中的文檔和其他幾個論壇上看到的。但是我需要把它放在左上角,我從來沒有發現或理解這種做法。此外,我沒有得到如何讓他們只要點擊一個參數就調用一個函數。
非常感謝您
編輯:我不知道爲什麼,但與「大家好」的第一行會被自動刪除
是可重複的將是相當不錯的一個例子。這裏的人沒有'MeshApp'和其他特殊的東西,如果你提供一個完整項目的鏈接,他們很可能不願意克隆一個回購站。小和可重複是快速和正確答案的關鍵。此外,還有一種「不喜」,「不感謝」的規則,我認爲它在[tour](http://stackoverflow.com/tour)中提到過。 :P – KeyWeeUsr
要放置在左上角,請在.kv文件中使用帶有pos_hint的RelativeLayout:{'top':1,'left':1} – Daniel