【发布时间】:2021-12-29 23:42:05
【问题描述】:
有没有办法激活/停用 wxpython 小部件的可编辑性,尤其是 FilePickerCtrl 小部件?
就我而言,我正在创建一个桌面应用程序,用户可以通过它启动和停止服务器。在启动服务器之前,用户可以通过 FilePickerCtrl 小部件指定一个变量,该变量将从服务器中使用。我想让 FilePickerCtrl 在服务器停止时可编辑,而在服务器运行时不可编辑。
到目前为止,我已经尝试了wxpython Validators,但没有成功。一些可重现的代码:
configuration.py(需要编辑excel路径)
global SERVER_CONF
SERVER_CONF = {
'XE_EXCEL_PATH': '/absolute/path/to/excel.xlsx'
}
gui_main.py
import wx
from server_tab import Server
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="")
# Create a panel and notebook (tabs holder)
p = wx.Panel(self)
nb = wx.Notebook(p)
# Create the tab windows
tab1 = Server(nb)
# Add the windows to tabs and name them.
nb.AddPage(tab1, tab1.name)
# Set noteboook in a sizer to create the layout
sizer = wx.BoxSizer()
sizer.Add(nb, 1, wx.EXPAND)
p.SetSizer(sizer)
if __name__ == "__main__":
app = wx.App()
MainFrame().Show()
app.MainLoop()
server.py
from flask import request
from flask import Flask
from configuration import SERVER_CONF
app = Flask(__name__)
@app.route('/', methods=['POST'])
def read_html_source():
global SERVER_CONF
print('HEREEE',SERVER_CONF['XE_EXCEL_PATH'])
return ('', 200)
@app.route("/shutdown", methods=['GET'])
def shutdown():
shutdown_func = request.environ.get('werkzeug.server.shutdown')
if shutdown_func is None:
raise RuntimeError('Not running werkzeug')
shutdown_func()
return ("Shutting down...", 200)
def main():
app.run(debug=False, port=5001)
if __name__ == "__main__":
main()
server_tab.py
import os
import requests
import threading
import wx
import server
from configuration import SERVER_CONF
# Define the tab content as classes:
class Server(wx.Panel):
name = "Server"
def __init__(self, parent):
global SERVER_CONF
self.is_server_active = False
wx.Panel.__init__(self, parent)
generic_sizer = wx.BoxSizer(wx.VERTICAL)
title = wx.StaticText(self, 0, "The APE API server")
generic_sizer.Add(title, 0, wx.ALL | wx.EXPAND, 5)
file_sizer = wx.BoxSizer(wx.HORIZONTAL)
excel_label = wx.StaticText(self, 0, "Excel ")
file_sizer.Add(excel_label, 0, wx.ALL | wx.EXPAND, 5)
self. excel_browser = wx.FilePickerCtrl(
self,
wildcard='*.xlsx',
path=SERVER_CONF['XE_EXCEL_PATH'],
validator=ServerValidator(self.is_server_active)
)
file_sizer.Add(self.excel_browser, 0, wx.ALL , 5)
generic_sizer.Add(file_sizer, 0, wx.ALL , 5)
server_btn_sizer = wx.BoxSizer(wx.HORIZONTAL)
start_server_btn = wx.Button(self, label='Run Server')
start_server_btn.Bind(wx.EVT_BUTTON, self.start_server)
server_btn_sizer.Add(start_server_btn, 0, wx.ALL | wx.EXPAND, 5)
stop_server_btn = wx.Button(self, label='Stop Server')
stop_server_btn.Bind(wx.EVT_BUTTON, self.stop_server)
server_btn_sizer.Add(stop_server_btn, 0, wx.ALL | wx.EXPAND, 5)
generic_sizer.Add(server_btn_sizer, 0, wx.ALL | wx.EXPAND, 5)
parent.SetSizer(generic_sizer)
def start_server(self, entry):
SERVER_CONF['XE_EXCEL_PATH'] = self.excel_browser.GetPath()
self.is_server_active = True
self.thread = threading.Thread(target=server.main)
self.thread.start()
def stop_server(self, entry):
resp = requests.get('http://localhost:5001/shutdown')
self.thread.join()
print('Server was stoped')
class ServerValidator(wx.PyValidator):
def __init__(self, is_server_active):
wx.Validator.__init__(self)
self.is_server_active = is_server_active
def Clone(self):
'''Required Validator method'''
return ServerValidator(self.is_server_active)
def Validate(self, win):
window = self.GetWindow()
print("Is running?", self.is_server_active)
return not self.is_server_active
使用python gui_main.py 运行并使用curl http://localhost:5001 -d POST 进行测试。如果您忘记从 GUI 中停止服务器,请运行 curl http://localhost:5001/shutdown
【问题讨论】:
-
self.excel_browser.Disable() ???
-
有效!谢谢!你会提交答案吗?我将重构我的问题