我是Kivy语言的新手。我正在尝试构建一个简单的程序来在两个屏幕之间切换。第一个屏幕包含一个按钮,该按钮的on_release将切换到第二个屏幕。单击第二个屏幕上的按钮将进入第一个屏幕。
问题我面对: 1.按钮放在角落,我希望它的大小可以全屏显示,但是很小
<ChatGUI>:
MainManager:
MainWindow:
SecondWindow:
<MainWindow>:
name: "main"
Button:
text:"to second window"
on_release:app.root.current="second"
<SecondWindow>:
name: "second"
Button:
text:"back to main"
on_release:app.root.current="main"
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.graphics import Rectangle, Color, Canvas
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.core.window import Window
from kivy.config import Config
from kivy.lang import Builder
class ChatGUI(Widget):
present=Builder.load_file("Chat.kv")
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class MainManager(ScreenManager):
pass
class ChatApp(App):
def build(self):
return ChatGUI()
if __name__=="__main__":
ChatApp().run()
我正在从youtube教程中练习。
我已经从堆栈溢出中检查了很多代码,但是我的代码中没有看到问题。
输出应显示占据整个窗口大小的按钮,on_release应切换到下一个屏幕。
可以让我知道可能是什么问题。
答案 0 :(得分:0)
您不需要在ScreenManager
内添加Widget
。
所以
class ChatGUI (ScreenManager):
在python文件和
中<ChatGUI>:
MainWindow:
SecondWindow:
在kv文件中 我所做的就是为了使您的示例有效而已。
Chat.kv
<ChatGUI>:
MainWindow:
SecondWindow:
<MainWindow>:
name: "main"
Button:
text:"to second window"
on_release:app.root.current="second"
<SecondWindow>:
name: "second"
Button:
text:"back to main"
on_release:app.root.current="main"
main.py
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.graphics import Rectangle, Color, Canvas
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.core.window import Window
from kivy.config import Config
from kivy.lang import Builder
class ChatGUI(ScreenManager):
present=Builder.load_file("Chat.kv")
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class ChatApp(App):
def build(self):
return ChatGUI()
if __name__=="__main__":
ChatApp().run()
答案 1 :(得分:0)
按钮放在角落,我希望它的大小已满 窗户但是很小
单击并释放按钮不会显示任何效果。
松开按钮时,屏幕未切换,因为该应用的根目录不是ScreenManager。
问题有两种选择。
此选项使用BoxLayout作为根,并需要进行以下增强。 Layout可以是GridLayout
,BoxLayout
,FloatLayout
等。
present = Builder.load_file(...)
替换为Builder.load_file(...)
Builder.load_file(...)
中移出class ChatGUI()
并添加pass
id: sm
下添加MainManager:
app.root.current
替换为app.root.ids.sm.current
main1.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
Builder.load_file("main1.kv")
class ChatGUI(BoxLayout):
pass
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class MainManager(ScreenManager):
pass
class ChatApp(App):
def build(self):
return ChatGUI()
if __name__ == "__main__":
ChatApp().run()
main1.kv
<ChatGUI>:
MainManager:
id: sm
MainWindow:
SecondWindow:
<MainWindow>:
name: "main"
Button:
text: "to second window"
on_release: app.root.ids.sm.current="second"
<SecondWindow>:
name: "second"
Button:
text: "back to main"
on_release: app.root.ids.sm.current="main"
此选项需要以下增强:
from kivy.uix.widget import Widget
class ChatGUI()
return ChatGUI()
替换为return MainManager()
present = Builder.load_file(...)
替换为Builder.load_file(...)
main2.py
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
Builder.load_file("main2.kv")
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class MainManager(ScreenManager):
pass
class ChatApp(App):
def build(self):
return MainManager()
if __name__ == "__main__":
ChatApp().run()
main2.kv
<MainManager>:
MainWindow:
SecondWindow:
<MainWindow>:
name: "main"
Button:
text: "to second window"
on_release: app.root.current="second"
<SecondWindow>:
name: "second"
Button:
text: "back to main"
on_release: app.root.current="main"