gpt4 book ai didi

python - Kivy:弹出窗口不显示

转载 作者:行者123 更新时间:2023-12-04 09:37:15 25 4
gpt4 key购买 nike

尽管我的示例代码不是最短的代码之一,但这是呈现我的应用程序内部工作的最少代码。
我的目标是保存更新进度条的弹出对话框(不是在这个例子中 - 不是问题)。
如果您尝试运行此代码,您将看到“保存”按钮 - 单击后会弹出显示。
但是我在 中呈现更新循环进度条()方法突然不再显示弹出窗口 - 为什么?
我正在互联网上寻找解决方案。起初我认为这与我用来在另一个线程中实际保存文件时更新弹出窗口的踩踏有关,但是这个例子没有线程并且实际方法被注释所以问题必须在其他地方。

import kivy
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from time import sleep

# Main app with save method
class MainApp(App):
def build(self):
sm = ScreenManager()
self.save = save(app=self, name='save')
sm.add_widget(self.save)
self.saved_percent = 0.0
return sm

def save_file(self,file_path):
for i in range(0,10):
sleep(1)
self.saved_percent = i * 10.0
print("saved",i)
self.save_finnished = True
return 256 # check for written bytes

# one of many screens - this one is for saving
class save(Screen):
def __init__(self, app, **kwargs):
super(Screen,self).__init__(**kwargs)
self.app = app
lyo = BoxLayout(orientation='vertical')
btn = Button(text="Save", size=(10,10))
btn.bind(on_press=self.begin_save)
lyo.add_widget(btn)
self.add_widget(lyo) # basic layout to init the save

def begin_save(self,*args):
self.app.save_finnished = False
self.cancel_save = False
file_path = 'foo.txt' # debug static
self.progress_bar(file_path) # TODO put in separate thread and continue
#save_file(self,file_path)

def progress_bar(self, file_path):
btn = Button(text="Cancel")
btn.bind(on_press=self.btn_cancel_save)

pop = Popup(title="Saving", content=btn, auto_dismiss=False)
pop.open() # does not open

# this loop is somehow causing the Popup not to display
#while (not self.app.save_finnished and not self.cancel_save):
# pass # progres bar is being updated here
#pop.dismiss() # dismiss after exiting the loop

def btn_cancel_save(self, *args):
print("Cancel clicked")
self.cancel_save = True


if __name__ == "__main__":
top=MainApp()
top.run()



















#
#import kivy
#from kivy.app import App
#from kivy.uix.screenmanager import ScreenManager, Screen
#from kivy.uix.boxlayout import BoxLayout
#from kivy.uix.progressbar import ProgressBar
#from kivy.uix.popup import Popup
#import threading
#from kivy.uix.label import Label
#from kivy.uix.button import Button
#from kivy.uix.boxlayout import BoxLayout
#from time import sleep
#from kivy.lang import Builder
#import queue
#
## Main app with save method
#class MainApp(App):
# def build(self):
# sm = ScreenManager()
# self.save = save(app=self, name='save')
# sm.add_widget(self.save)
# self.saved_percent = 0.0
# return sm
#
# def save_file(self,file_path):
# for i in range(0,10):
# sleep(1)
# self.saved_percent = i * 10.0
# print("saved",i)
# return 256 # check for written bytes
#
## one of many screens - this one is for saving
#class save(Screen):
# def __init__(self, app, **kwargs):
# super(Screen,self).__init__(**kwargs)
# self.app = app
# lyo = BoxLayout(orientation='vertical')
# btn = Button(text="Save", size=(10,10))
# btn.bind(on_press=self.begin_save)
# lyo.add_widget(btn)
# self.add_widget(lyo) # basic layout to init the save
#
# def begin_save(self,*args):
# self.save_finnished = False
# self.cancel_save = False
# file_path = 'foo.txt' # debug static
# #written = self.poi.save_file(file_path)
# #self.progress_bar(file_path)
#
# # some black magic with threads to get ret value
# # https://www.edureka.co/community/31966/how-to-get-the-return-value-from-a-thread-using-python
# #que = queue.Queue()
# #t = threading.Thread(target=lambda q, arg1: q.put(self.app.save_file(arg1)), args=(que, file_path))
# #t.start()
# self.progress_bar(file_path)
# #progress_bar_thread = threading.Thread(target=self.progress_bar, args=(file_path,))
# #progress_bar_thread.start()
# #t.join()
# #written = que.get()
#
## self.save_finnished = True
## size = os.path.getsize(file_path)
## if(not self.btn_cancel_save):
## if(written == size):
## print("OK")
## self.ok_dialog(True)
## else:
## print("Error saving to POI")
## self.ok_dialog(False)
#
# def progress_bar(self, file_path):
# print("progress bar fnc ")
# #lyo = BoxLayout(orientation='vertical')
# #pb = ProgressBar(max=100.0)
# #lbl = Label(text='0 %')
# btn = Button(text="Cancel")
# btn.bind(on_press=self.btn_cancel_save)
# #lyo.add_widget(Label(text=file_path))
# #lyo.add_widget(pb)
# #lyo.add_widget(lbl)
# #lyo.add_widget(btn)
#
# #pop = Popup(title="Saving", content=lyo, auto_dismiss=False)
# pop = Popup(title="Saving", content=btn, auto_dismiss=False)
# pop.open() # does not open
#
## print("start update loop")
## print("finished?",self.save_finnished, "cancel?" ,self.cancel_save)
# while (not self.save_finnished or not self.cancel_save):
# pass # progres bar is being updated here
## pb.value = self.app.saved_percent
## s = '%.12f' % self.app.saved_percent
## i, p, d = s.partition('.')
## str_percent = '.'.join([i, (d+'0'*2)[:2]])
## lbl.text = str_percent +' %'
## #print(" # ")
# print("out of update loop - dismiss pop")
# pop.dismiss()
#
# def ok_dialog(self,success):
# if(success):
# title = "SUCCESS"
# else:
# title = "FAILED"
# btn = Button(text="OK")
#
# pop = Popup(title=title, content=btn, auto_dismiss=False)
# btn.bind(on_press=pop.dismiss)
# pop.open()
#
#
# def btn_cancel_save(self, *args):
# print("Cancel clicked")
# self.cancel_save = True
#
#
#if __name__ == "__main__":
# top=MainApp()
# top.run()

最佳答案

使用线程我会使用这样的东西;
您必须退出按钮回调,否则 eventloop 不会重绘小部件。

import time
from threading import Thread
from kivy.uix.progressbar import ProgressBar
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout


class TestApp(App):
def build(self):
self.thread = None

parent = Widget()
btn = Button(text='copy file', on_press=self.start_thread_copy_file)
parent.add_widget(btn)
return parent

def start_thread_copy_file(self, *args):
if self.thread is None:
# open progressbar window
self.progress_bar = ProgressBar(max=5)
btn = Button(text="Cancel")
btn.bind(on_press=self.thread_copy_cancel)

layout = BoxLayout()
layout.add_widget(self.progress_bar)
layout.add_widget(btn)

self.pop = Popup(title="Saving", content=layout)
self.pop.open()

self.thread = Thread(target=self.thread_copy_file_func)
self.thread.start()

def thread_copy_cancel(self, *args):
self.thread = None # can be also done with a flag
self.pop.dismiss()

def thread_copy_file_func(self):
try:
for i in range(5):
print('thread_copy_file_func i =', i)
self.progress_bar.value = i
time.sleep(1)
if self.thread is None: # cancel condition
break

finally:
self.thread = None
self.pop.dismiss()


TestApp().run()

关于python - Kivy:弹出窗口不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62513334/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com