Kivy python创建一个标签栏

时间:2017-12-21 19:30:13

标签: python kivy

我从kivy开始,我一般都了解主要概念(它与tkinter非常相似)

但有一些我不能做的事情,我想在我的窗口顶部创建一个标签栏(就像在网络浏览器中一样)

我发现的唯一教程看起来不像我的代码:https://kivy.org/docs/api-kivy.uix.tabbedpanel.html

我的代码:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
class MyApp(App):
# layout
    def build(self):
        layout = BoxLayout(orientation='vertical')
        # use a (r, g, b, a) tuple
        blue = (0, 0, 1.5, 2.5)
        red = (2.5, 0, 0, 1.5)

        btn =  Button(text='Touch me!', background_color=blue, font_size=120)
        btn.bind(on_press=self.callback)
        btn2 =  Button(text='btn2', background_color=red, font_size=120)
        layout.add_widget(btn)
        layout.add_widget(btn2)

        self.label = Label(text="------------", font_size='50sp')
        layout.add_widget(self.label)

        btn1 = Button(text="OK")
        btn1.bind(on_press=self.buttonClicked)
        layout.add_widget(btn1)
        self.lbl1 = Label(text="test")
        layout.add_widget(self.lbl1)
        self.txt1 = TextInput(text='', multiline=False)
        layout.add_widget(self.txt1)
        return layout

    def callback(self, event):
        print("button touched")  # test
        self.label.text = "button touched"

# button click function
    def buttonClicked(self,btn):
        self.lbl1.text = "You wrote " + self.txt1.text

# run app
if __name__ == "__main__":
    MyApp().run()
 # join all items in a list into 1 big string

我尝试添加tabpannel但我有两个问题: 1)如何更改标签名称?并删除“defaulttab” 2)为什么我的小部件不在我的标签中?

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.tabbedpanel import TabbedPanelItem

class MyApp(App):
# layout
    def build(self):
        layout = BoxLayout(orientation='vertical')
        # use a (r, g, b, a) tuple
        blue = (0, 0, 1.5, 2.5)
        red = (2.5, 0, 0, 1.5)


        tab=TabbedPanel()
        tabitem=TabbedPanelItem()
        tab.add_widget(tabitem)
        layout.add_widget(tab)

        btn =  Button(text='Touch me!', background_color=blue, font_size=120)
        btn.bind(on_press=self.callback)
        btn2 =  Button(text='btn2', background_color=red, font_size=120)
        tabitem.add_widget(btn)
        tabitem.add_widget(btn2)

        self.label = Label(text="------------", font_size='50sp')
        tabitem.add_widget(self.label)

        btn1 = Button(text="OK")
        btn1.bind(on_press=self.buttonClicked)
        tabitem.add_widget(btn1)
        self.lbl1 = Label(text="test")
        tabitem.add_widget(self.lbl1)
        self.txt1 = TextInput(text='', multiline=False)
        tabitem.add_widget(self.txt1)

        return layout

    def callback(self, event):
        print("button touched")  # test
        self.label.text = "button touched"

# button click function
    def buttonClicked(self,btn):
        self.lbl1.text = "You wrote " + self.txt1.text

# run app
if __name__ == "__main__":
    MyApp().run()
 # join all items in a list into 1 big string

0 个答案:

没有答案