2016-05-31 80 views
0

我正在與kivy一個應用程序,我有一個問題,涉及GridLayout。我有一個不同行的屏幕,我希望最後一行的按鈕始終具有相同的屏幕高度的11.1%。我試圖修改按鈕中的屬性高度,但無法正常工作。與size_hint_y工作正常,但事實是,我想要height,因爲屏幕將不總是相同數量的行(是響應和它取決於以前的屏幕選擇)。我在這裏附上我已經通過命令Window.height/9計算的屬性高度完成的代碼:如何在Kivy Python中爲GridLayout的某些按鈕設置高度?

from kivy.app import App 
from kivy.uix.label import Label 
from kivy.uix.gridlayout import GridLayout 
from kivy.uix.textinput import TextInput 
from kivy.uix.button import Button 
from kivy.uix.screenmanager import ScreenManager, Screen 
from kivy.core.window import Window 


class LoginScreen(GridLayout): 
    def __init__(self,**kwargs): 
     super(LoginScreen, self).__init__(**kwargs) 
     self.cols=2 
     self.add_widget(Label(text='Subject')) 
     self.add_widget(Label(text='')) 
     self.add_widget(Label(text='1')) 
     self.add_widget(TextInput(multiline=False)) 
     self.add_widget(Label(text='2')) 
     self.add_widget(TextInput(multiline=False)) 
     self.add_widget(Label(text='3')) 
     self.add_widget(TextInput(multiline=False)) 
     self.add_widget(Label(text='4')) 
     self.add_widget(TextInput(multiline=False)) 
     b1=Button(text='Exit',background_color=[0,1,0,1],height=int(Window.height)/9.0) #doesn't work properly 
     self.add_widget(b1) 
     b2=Button(text='Run',background_color=[0,1,0,1],height=int(Window.height)/9.0) #doesn't work properly 
     self.add_widget(b2) 
     b1.bind(on_press=exit) 




class SimpleKivy(App): 
    def build(self): 
     return LoginScreen() 


if __name__=='__main__': 
    SimpleKivy().run() 

我知道這可能與kivy語言在一個更簡單的方法,但我的應用程序是更好地做這個做辦法。如果有人知道如何解決這個問題,我將非常感激。

回答

1

如果你想在一個網格/盒佈局的窗口小部件有一個固定的大小,你應該設置其size_hintNone第一。在這些任務中總是使用kivy lang - 沒有例外。

from kivy.app import App 
from kivy.uix.screenmanager import Screen 

from kivy.lang import Builder 

gui = ''' 
LoginScreen: 

    GridLayout: 
     cols: 2 

     Label: 
      text: 'Subject' 

     Label: 

     Label: 
      text: '1' 

     SingleLineTextInput: 

     Label: 
      text: '2' 

     SingleLineTextInput: 

     Label: 
      text: '3' 

     SingleLineTextInput: 

     Label: 
      text: '4' 

     SingleLineTextInput: 

     GreenButton: 
      text: 'Exit' 
      on_press: app.stop() 

     GreenButton: 
      text: 'Run' 


<[email protected]>: 
    multiline: False 


<[email protected]>: 
    background_color: 0, 1, 0, 1 
    size_hint_y: None 
    height: self.parent.height * 0.111 
''' 


class LoginScreen(Screen): 
    pass 


class SimpleKivy(App): 

    def build(self): 
     return Builder.load_string(gui) 


if __name__ == '__main__': 
    SimpleKivy().run() 
+0

事實是,我正在使用python語言而不是kivy語言,因爲我更喜歡開發該應用程序。網格佈局屏幕是使用之前選擇的以前屏幕構建的,保存在迭代的列表中以創建最終屏幕。我想要用kivy語言,但我不知道如何讀取例如7個元素的列表,並使用kivy語言創建7行,每行都有一個標籤和一個文本輸入。最後我決定用Python語言來完成它。你的答案對我有很大的幫助,我永遠不會認爲將'size_hint'設置爲'None'是解決方案。 –

0

試試這個

class LoginScreen(GridLayout): 
def __init__(self,**kwargs): 
    super(LoginScreen, self).__init__(**kwargs) 
    self.cols=2 
    self.add_widget(Label(text='Subject')) 
    self.add_widget(Label(text='')) 
    self.add_widget(Label(text='1')) 
    self.add_widget(TextInput(multiline=False)) 
    self.add_widget(Label(text='2')) 
    self.add_widget(TextInput(multiline=False)) 
    self.add_widget(Label(text='3')) 
    self.add_widget(TextInput(multiline=False)) 
    self.add_widget(Label(text='4')) 
    self.add_widget(TextInput(multiline=False)) 
    b1=Button(text='Exit',background_color=[0,1,0,1],size_hint_y=None, height=int(Window.height)/8.9) 
    self.add_widget(b1) 
    b2=Button(text='Run',background_color=[0,1,0,1],size_hint_y=None, height=int(Window.height)/8.9) 
    self.add_widget(b2) 
    b1.bind(on_press=exit) 

編輯,以改變它是在11%。

這裏的一個按鈕保持在11%的響應窗口大小,每當窗口被調整大小時重繪網格層(通過綁定到'on_resize')。

from kivy.app import App 
from kivy.uix.label import Label 
from kivy.uix.gridlayout import GridLayout 
from kivy.uix.textinput import TextInput 
from kivy.uix.button import Button 
from kivy.uix.screenmanager import ScreenManager, Screen 
from kivy.core.window import Window 
from kivy.uix.floatlayout import FloatLayout 


class LoginScreen(GridLayout): 


def __init__(self,**kwargs): 
    super(LoginScreen, self).__init__(**kwargs) 

    #init and add grid layer 
    self.cols=2 
    self.layout = GridLayout(cols=self.cols) 
    self.add_widget(self.layout) 
    #function to set the buttons based on the current window size 
    self.set_content(Window.width, Window.height) 
    #bind above function to get called whenever the window resizes 
    Window.bind(on_resize=self.set_content) 


def set_content(self, width, height, *args): 
    #first remove the old sized grid layer 
    self.remove_widget(self.layout) 
    #now build a new grid layer with the current size 
    self.layout =GridLayout(cols=self.cols) 
    self.layout.add_widget(Label(text='Subject')) 
    self.layout.add_widget(Label(text='')) 
    self.layout.add_widget(Label(text='1')) 
    self.layout.add_widget(TextInput(multiline=False)) 
    self.layout.add_widget(Label(text='2')) 
    self.layout.add_widget(TextInput(multiline=False)) 
    self.layout.add_widget(Label(text='3')) 
    self.layout.add_widget(TextInput(multiline=False)) 
    self.layout.add_widget(Label(text='4')) 
    self.layout.add_widget(TextInput(multiline=False)) 
    b1=Button(text='Exit',background_color=[0,1,0,1],size_hint_y=None, height=int(Window.height)/8.9) 
    self.layout.add_widget(b1) 
    b2=Button(text='Run',background_color=[0,1,0,1],size_hint_y=None, height=int(Window.height)/8.9) 
    self.layout.add_widget(b2) 
    b1.bind(on_press=exit) 
    #add the newly sized layer 
    self.add_widget(self.layout) 


class SimpleKivy(App): 
def build(self): 
    return LoginScreen() 


if __name__=='__main__': 
SimpleKivy().run() 
+0

我修改了te代碼,因爲它錯了。這兩個屬性是高度,我試過如果它運行良好,我得到了同樣的結果。我想我正在做一些錯誤的事情。 –

+0

我明白了,所以上面的代碼應該將按鈕的大小設置爲窗口大小的11%。如果你想改變它(比如你的窗口的一半用於別的東西,而你想用半個窗口的11%),那麼你需要更新按鈕的大小,然後你需要單獨做函數(而不是__init__)。您可以刪除您的登錄屏幕並使用新維度創建一個新的維度,但這不是最乾淨的。我目前正在處理同樣的問題.. – ninehundred

+0

這比這更容易。只有我希望屏幕底部的兩個按鈕爲屏幕的1/9。我不關心其他領域('標籤'和'textinputs')。我曾嘗試設置像素值(例如'50px'),但我得到了相同的結果... –

相關問題