这类似于有关Kivy早期版本的问题:Kivy Popup rendering issue 但没有找到有用的答案。
这是我使用Kivy 1.9.1和Python 2.7.12在Windows 10下使用Popup的问题的简化版本。每次弹出Popup时都不会出现此问题,但大约50%的时间都会出现此问题。症状是弹出窗口的布局有时是不正确的,有时甚至是不正确的(即,按钮文本甚至不在按钮上,或者标题甚至不在弹出窗口内)。我正在使用Thread和Queue等待Popup完成。要查看问题,请运行kivyplay.py脚本,单击“开始游戏线程”,单击弹出窗口中的“确定”按钮。弹出窗口将出现4次,如果问题没有出现,您可以再次单击“开始游戏线程”。我看到的警告信息是:
[WARNING ] <kivy.uix.gridlayout.GridLayout object at 0x000000000ADDF118> have no cols or rows set, layout is not triggered.
我没有直接使用任何GridLayout小部件。这个相同的代码在Ubuntu 16.04下使用相同版本的Kivy和Python完美无缺地运行。
我尝试了很多不同的方法:
使用ModalView可以消除GridLayout警告,但不能解决问题。看起来弹出布局越复杂,错误出现的次数越多,因此这个简单的示例不会经常显示错误。难道我做错了什么?关于如何让弹出窗口正确渲染的任何想法?我应该将此报告为错误吗?
感谢!!!
kivyplay.kv:
from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from game_thread import GameThread
class KivyPlay(RelativeLayout):
def __init__(self, *args):
RelativeLayout.__init__(self, *args)
def start_thread(self):
print "Got a click"
gameThread = GameThread()
gameThread.start()
def quit(self):
app.stop()
class PlayApp(App):
def build(self):
return KivyPlay()
if __name__ == '__main__':
app = PlayApp()
app.run()
game_thread.py:
from threading import *
from Queue import Queue
from my_popup import MyPopup
class GameThread(Thread):
def __init__(self):
Thread.__init__(self, name="GameThread")
self.daemon = True
self.queue = Queue()
def run(self):
for i in range(0,4):
popup = MyPopup("Popup Test", self.queue, i)
popup.open()
popReturn = self.queue.get(True)
print "Popup #" + str(i) + " returned " + str(popReturn)
my_popup.ky:
from kivy.uix.popup import Popup
from kivy.lang import Builder
class MyPopup(Popup):
def __init__(self, theTitle, theQueue, playerNumber):
self.queue = theQueue
title = theTitle
self.player = playerNumber
myContent = Builder.load_file('pop_test.kv')
self.button = myContent.ids["ok_button"]
self.button.bind(on_press=self.okButton)
Popup.__init__(self, title=title, title_align='center', content=myContent, size_hint=(None, None), size=(400,150), auto_dismiss=False)
def okButton(self, *args):
self.queue.put(self.player, False)
self.dismiss()
play.kv:
#:kivy 1.9.1
<KivyPlay>:
canvas:
Color:
rgba: 1, 0, 0, 1
Rectangle:
pos: self.pos
size: self.size
Button:
size_hint: 0.25, 0.25
pos_hint: {'center_x': 0.5, 'y': 0.5}
text: 'Start Game Thread'
on_press: root.start_thread()
Button:
size_hint: 0.25, 0.25
pos_hint: {'center_x': 0.5, 'y': 0.25}
text: 'Quit'
on_press: root.quit()
pop_test.kv:
#:kivy 1.9.1
BoxLayout:
orientation: 'vertical'
AnchorLayout:
anchor_x: 'center'
anchor_y: 'center'
Button:
id: ok_button
size_hint: 0.2, 0.2
text: 'OK'
答案 0 :(得分:0)
运行super()
时,您需要在顶部调用__init__
,以确保在开始覆盖课程中的内容之前启动小部件。
所以我改变了你的三个文件。我没有看到任何错误(在胜利10上测试)。您可以忽略gridlayout警告。
kivy_play.py:
from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from game_thread import GameThread
class KivyPlay(RelativeLayout):
def __init__(self, **kwargs):
super(KivyPlay,self).__init__(**kwargs)
def start_thread(self):
print "Got a click"
gameThread = GameThread()
gameThread.start()
def quit(self):
app.stop()
class PlayApp(App):
def build(self):
return KivyPlay()
if __name__ == '__main__':
app = PlayApp()
app.run()
和my_popup.py:
from kivy.uix.popup import Popup
from kivy.lang import Builder
Builder.load_file('pop_test.kv')
class MyPopup(Popup):
def __init__(self, theTitle, theQueue, playerNumber,**kwargs):
super(MyPopup,self).__init__(**kwargs)
self.title = theTitle
self.title_align='center'
self.queue = theQueue
self.player = playerNumber
self.size_hint=(None, None)
self.size=(400,150)
self.auto_dismiss=False
def okButton(self, *args):
self.queue.put(self.player, False)
self.dismiss()
pop_test.kv:
#:kivy 1.9.1
<MyPopup>:
BoxLayout:
orientation: 'vertical'
AnchorLayout:
anchor_x: 'center'
anchor_y: 'center'
Button:
id: ok_button
size_hint: 0.2, 0.2
text: 'OK'
on_press:root.okButton()