您正在开发一个程序,需要在运行时获取大量参数。您使用 wxGlade 构建了一个 GUI,希望在应用程序中获取这些参数。您不想使用 INI 文件,而是希望通过 GUI 来控制应用程序。您还希望能够在 GUI 中保存值,以便用户无需每次都重新输入。
解决方案
您可以使用 Python 的 wx 模块来实现这一目标。wx 模块提供了一系列类和函数,可以帮助您构建复杂的 GUI 应用程序。
在您的代码中,您已经创建了一个 MyFrame 类,该类继承自 wx.Frame 类。MyFrame 类包含了一个 kartei 变量,kartei 是一个 Notebook 对象,用于存储多个选项卡。每个选项卡都包含一个面板,面板上放置了各种各样的控件,如文本框、按钮和复选框。
为了获取 GUI 中的值,您可以在控件上绑定事件处理程序。例如,您可以为文本框绑定一个键盘事件处理程序,并在用户更改文本框中的文本时触发该事件处理程序。在事件处理程序中,您可以获取文本框的值并将其存储在变量中。
您还可以使用 wx.StaticText 类来创建静态文本。静态文本不能被编辑,但可以显示文本。您可以使用静态文本来向用户显示信息,例如,您可以在静态文本中显示参数的名称和值。
此外,您还可以使用 wx.Button 类来创建按钮。按钮可以被点击,当用户点击按钮时,可以触发一个事件处理程序。在事件处理程序中,您可以获取 GUI 中的值并将其传递给程序。
以下是获取 GUI 中的值的代码示例:
import wx
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
# ...
# Create a text control and bind a keyboard event handler to it
self.text_ctrl = wx.TextCtrl(self.panel)
self.text_ctrl.Bind(wx.EVT_TEXT, self.on_text_ctrl_change)
# Create a static text control and set its value
self.static_text = wx.StaticText(self.panel, label="Parameter value:")
# Create a button and bind a click event handler to it
self.button = wx.Button(self.panel, label="Get value")
self.button.Bind(wx.EVT_BUTTON, self.on_button_click)
def on_text_ctrl_change(self, event):
# Get the value of the text control
value = self.text_ctrl.GetValue()
# Set the value of the static text control
self.static_text.SetLabel("Parameter value: {}".format(value))
def on_button_click(self, event):
# Get the values of the GUI controls
abrjahr = self.abr_jahr.GetValue()
zuschlag = self.zuschlag.GetValue()
fehlzeit = self.fehlzeit.GetValue()
urlaub = self.urlaub.GetValue()
soll = self.soll.GetValue()
# Pass the values to the program
self.program.set_parameters(abrjahr, zuschlag, fehlzeit, urlaub, soll)
代码例子
以下是完整的代码示例,演示如何从 GUI 中获取变量并传递给程序:
import wx
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
# ...
# Create a text control and bind a keyboard event handler to it
self.text_ctrl = wx.TextCtrl(self.panel)
self.text_ctrl.Bind(wx.EVT_TEXT, self.on_text_ctrl_change)
# Create a static text control and set its value
self.static_text = wx.StaticText(self.panel, label="Parameter value:")
# Create a button and bind a click event handler to it
self.button = wx.Button(self.panel, label="Get value")
self.button.Bind(wx.EVT_BUTTON, self.on_button_click)
def on_text_ctrl_change(self, event):
# Get the value of the text control
value = self.text_ctrl.GetValue()
# Set the value of the static text control
self.static_text.SetLabel("Parameter value: {}".format(value))
def on_button_click(self, event):
# Get the values of the GUI controls
abrjahr = self.abr_jahr.GetValue()
zuschlag = self.zuschlag.GetValue()
fehlzeit = self.fehlzeit.GetValue()
urlaub = self.urlaub.GetValue()
soll = self.soll.GetValue()
# Pass the values to the program
self.program.set_parameters(abrjahr, zuschlag, fehlzeit, urlaub, soll)
class Program:
def __init__(self):
self.abrjahr = None
self.zuschlag = None
self.fehlzeit = None
self.urlaub = None
self.soll = None
def set_parameters(self, abrjahr, zuschlag, fehlzeit, urlaub, soll):
self.abrjahr = abrjahr
self.zuschlag = zuschlag
self.fehlzeit = fehlzeit
self.urlaub = urlaub
self.soll = soll
def run(self):
# Use the parameters to perform some calculations or other operations...
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MyFrame()
frame.Show()
app.MainLoop()
program = Program()
program.run()