python - Kivy 弹出窗口显示与主屏幕相同的按钮
问题描述
我对 Kivy 很陌生(已经使用了大约四个小时......)而且我遇到了弹出窗口。
我有一个主屏幕,在浮动布局中有四个按钮。按下时,我希望“移动”按钮打开一个弹出窗口。现在我已经完成了这项工作,但弹出窗口包含与我的主屏幕相同的四个按钮。
这是我的 Python 代码:
def show_movepop():
show = MovePop()
movepopWindow = Popup(title="Move", content=show, size_hint=(None, None),size=(400,400))
movepopWindow.open()
class MovePop(FloatLayout):
pass
class MainWindow(Screen):
def movebtn(self):
show_movepop()
class StatsWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("gamegui.kv")
class MainFloatApp(App):
def build(self):
return kv
if __name__ == "__main__":
MainFloatApp().run()
这是我的 .kv 文件:
WindowManager:
MainWindow:
StatsWindow:
<Button>
font_size:40
color:0.3,0.6,0.7,1
size_hint: 0.5, 0.1
<MainWindow>:
name: "mainscreen"
FloatLayout
Button:
text: "MOVE"
id: move
pos_hint: {"x":0, "y":0.1}
on_release: root.movebtn()
Button:
text: "ACTION"
id: action
pos_hint: {"x":0.5, "y":0.1}
Button:
text: "EXAMINE"
id: examine
pos_hint: {"x":0, "y":0}
Button:
text: "STATS"
id: stats
pos_hint: {"x":0.5, "y":0}
on_release:
app.root.current = "statsscreen"
root.manager.transition.direction = "left"
<StatsWindow>:
name: "statsscreen"
Button:
text: "Back"
on_release:
app.root.current = "mainscreen"
root.manager.transition.direction = "right"
<MovePop>:
Button:
text: "!"
pos_hint: {"x":0.1, "y":0.5}
on_release:
如果以上内容非常肮脏,请提前道歉,我效率不高:')
所有建议表示赞赏!
解决方案
好的,所以我不知道为什么,但这FloatLayout
是导致问题的原因。
改变了
class MovePop(FloatLayout):
pass
到:
class MovePop(AnchorLayout):
pass
BoxLayout
也摆脱了重复的按钮,但我无法按照我在该布局中想要的方式排列弹出窗口上的内容。
推荐阅读
- c# - 功能
不接受 2 个参数,但文档另有说明? - javascript - 如何使用 window.scrollTo 在元素内滚动?
- javascript - 如何在angularjs中根据div id展开和折叠一个div
- sql - SQL Server - 是否创建具有唯一约束的索引作为必要的列之一?
- github - 我怎样才能制作这样的 README 结构?
- python - Python - 为什么 a=1/3 是 int 而 a=0.3 是 float
- r - 如何计算变量超过其阈值的次数。忽略连续事件
- powershell - 如何为 PowerShell 安装 GroupPolicy 模块?
- ionic-framework - Ionic 4 在目录中创建项目
- php - php7 打印 html 表格(条形码)到网络打印机