所以,我正在做的GUI具有主屏幕和加载屏幕。使用加载屏幕,以便用户在程序执行功能时可以看到应用程序的进度。我希望程序更改为执行功能,并在执行功能后切换回主屏幕。这是我的代码的一部分,可用于最终运行该程序。
# Imports
import kivy
from kivy.app import App # Imports functions to build the app
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.image import Image
import threading
import time
kivy.require('1.10.1')
def function1():
time.sleep(5)
print("Done")
def function2():
time.sleep(5)
print("Done 2")
class MainScreen(Screen, FloatLayout): # Main Screen Class that holds every method in the Start Screen
auction_name = " "
default = "Select an auction"
selected_auction = StringProperty('Select an auction')
x_value = 450
y_value = 500
def change_label(self, change): # Changes the text that shows what auction is selected
strtst = "Selected: " + change
if self.selected_auction == self.default and (change == "Copart" or change == "IAAI"):
self.selected_auction = "Selected: " + change
self.selected_auction = "Selected: " + change
elif self.selected_auction == strtst or self.selected_auction == "Main":
self.selected_auction = self.default
elif change != self.selected_auction:
if self.selected_auction == "Selected: Copart and IAAI":
self.selected_auction = "Selected: " + change
else:
if change == "None":
self.selected_auction == "TO START, PLEASE SELECT AN AUCTION"
else:
self.selected_auction = "Selected: Copart and IAAI"
def change_screen(self):
self.manager.current = "Loading Screen"
t2 = threading.Thread(target=function1)
t3 = threading.Thread(target=function2)
def start_process(self): # Used to start the web scraping
if self.selected_auction == 'Selected: Copart':
self.manager.current = "Loading Screen"
self.t3.start()
self.manager.current = "Results Screen"
self.selected_auction = "Select an auction"
elif self.selected_auction == 'Selected: IAAI':
self.manager.current = "Loading Screen"
self.t2.start()
self.selected_auction = "Select an auction"
elif self.selected_auction == 'Selected: Copart and IAAI':
self.manager.current = "Loading Screen"
self.t2.start()
self.t3.start()
else:
self.change_label("None")
class LoadingScreen(Screen):
def cancel(self):
box = BoxLayout(orientation='vertical')
padding = Label(text= "If you say yes all progress will be lost!")
buttons = BoxLayout()
warning = Image(source="Data/warning.png")
yes_button = Button(text= "Yes, do cancel it.", on_release=lambda x:self.back(),
on_press=lambda *args: pop.dismiss(), size=(250, 120), background_color=(300, 0.5, 0, .7))
no_button = Button(text="No, don't cancel.", on_press=lambda *args: pop.dismiss(), size=(250, 120),
background_color=(.25, 150, .04, 0.5))
box.add_widget(warning)
box.add_widget(padding)
buttons.add_widget(yes_button)
buttons.add_widget(no_button)
box.add_widget(buttons)
pop = Popup(title="Do you wish to cancel?", content=box, size_hint=(None, None), size= (800, 500))
pop.open()
class Manager(ScreenManager):
main_screen = ObjectProperty(None)
loading_screen = ObjectProperty(None)
class MainApp(App):
choice = ""
source = StringProperty(None)
def build(self):
m = Manager(transition=NoTransition())
return m
if __name__ == "__main__":
Window.clearcolor = (.46875, .46875, .4765, 1)
Window.size = (850, 1000)
MainApp().run()
在主屏幕中,有一个按钮,按下该按钮即可更改屏幕并执行功能,该按钮使用start_process()
处理。这是我的main.kv文件:
#: import NoTransition kivy.uix.screenmanager.NoTransition
#: import ProgressBar kivy.uix.progressbar
#: import Widget kivy.uix.widget
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import ListItemButton kivy.uix.listview.ListItemButton
#: import Popup kivy.uix.popup
#: import main gui_test
<Manager>:
id: screen_manager
main_screen: mainS
loading_screen: loadingS
MainScreen:
id: mainS
name: "Main Screen"
manager: screen_manager
LoadingScreen:
id: loadingS
name: "Loading Screen"
manager: screen_manager
<MainScreen>
FloatLayout:
# Start Button
Button:
on_release: root.start_process()
text: "Start"
font_size: 16
size_hint: 0.6, 0.12
pos: root.center_x - (self.width/2), root.y + self.height
background_normal: ''
background_down:
background_color: .25, .75, .04, 1
font_size: 50
# COPART BUTTON
Button:
size_hint: 0.279, 0.3
pos: root.x + (0.3 * self.width), root.top - self.height - 160
on_press: root.change_label("Copart")
# IAAI BUTTON
Button:
id: IAAI_Button
size_hint: 0.27, 0.3
pos: root.x + (1.7 * self.width) + 300, root.top - self.height - 160
on_press: root.change_label("IAAI")
Label:
text: root.selected_auction
font_size: 50
<LoadingScreen>
FloatLayout:
Button:
text: "Cancel"
size_hint:0.30, 0.1
pos: root.center_x - (self.width/2), 100
background_color: 300, 0.5, 0, .4
on_press: root.cancel()
您对我做错了什么建议吗?谢谢您的帮助!
答案 0 :(得分:0)
这是运行功能后返回主屏幕的一种方法。将目标函数修改为:
def function1():
time.sleep(5)
print("Done")
Clock.schedule_once(App.get_running_app().show_main_screen)
def function2():
time.sleep(5)
print("Done 2")
Clock.schedule_once(App.get_running_app().show_main_screen)
Clock.schedule_once()
计划在主线程上运行某些内容(需要更改UI)。
并将MainApp
修改为:
class MainApp(App):
choice = ""
source = StringProperty(None)
def build(self):
self.m = Manager(transition=NoTransition()) # save a reference to `Manager`
return self.m
def show_main_screen(self, dt):
self.m.current = "Main Screen" # use the saved reference to change the current screen
此外,在您的“ kv”文件中,以下行:
manager: screen_manager
不需要。该属性会自动添加到每个Screen
中。