我是编码的新手,正在努力将变量输入“ frameA”输入输出到“ frameB”。我的问题是,应如何编写代码的布局,以使输入到FrameA的变量可用(并在用户输入时刷新-通过单击按钮或取消选择textctrl等)在frameB中?
我正在使用wxpython,并以Wx Builder为指导,但是编写实际的UI代码时,我自己却可以帮助理解其底层内容。我试过了class-> def-> frame UI,按照我想要的方式组织了这些框架,根据需要显示和隐藏,并且我想要的变量可以在该框架内单击按钮时打印出来,但是无法从该外部访问框架,即使我正在“返回”所需的变量。
我暂时不在电脑上,可以的话,我可以在这里上传代码。现在我的理解是:(其中frameB会显示原始VariA消息,直到FrameA事件更新它为止)
VariA = ‘I want this to be changed to say moose in frameB’
Def frameA
Frame A ui info
On some event VariA = ‘moose’
Return VariA
Layout()
Def frameB
Frame B ui info
Statictext label somewhere in the UI is VariA
由于我还是新手,所以我可以理解每一行代码,但是更大的组织仍然对我很混乱。 如果代码结构在设计上是所有框架和面板等,则具有自己的类/定义,“主代码”中的类/定义之外的所有变量都将在类/定义中调用并更新,然后返回。我知道这是对代码的一种超级简单的思考方式,有人可以为此推荐任何好的阅读材料来帮助我“点击”吗? 谢谢,日落
答案 0 :(得分:1)
如果您希望所有类均可访问var
的值,则需要在分配新的global var
的方法InputUpdate()
中将行Class FrameA
添加到var
中的方法var
值var
。
但是,这将仅使import wx
var = "This Should change to whatever is typed in FrameA"
class FrameA ( wx.Frame ):
def __init__(self, parent):
wx.Frame.__init__ (self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=(0,0), size=(500,300), style=wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL)
bSizer1 = wx.BoxSizer(wx.HORIZONTAL)
self.INPUT = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
bSizer1.Add(self.INPUT, 1, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
self.SetSizer(bSizer1)
self.INPUT.Bind(wx.EVT_TEXT, self.InputUpdate)
def InputUpdate(self, Evt):
#### You need to add the following line before assigning the new value to
#### var in order to modify the global var variable and not the local
#### var variable
global var
var = self.INPUT.GetValue()
#print (var)
#### The try/except block is placed to avoid having an error if the text
#### in frameA changes but frameB does not exist anymore. The code inside
#### the try statement changes the text in frameB in real time.
try:
frmB.OUTPUT.SetLabel(var)
except Exception:
pass
class FrameB ( wx.Frame ):
def __init__(self, parent):
wx.Frame.__init__ (self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=(100,100), size=(500,300), style=wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL)
bSizer2 = wx.BoxSizer(wx.VERTICAL)
self.OUTPUT = wx.StaticText(self, wx.ID_ANY, var, wx.DefaultPosition, wx.DefaultSize, 0)
bSizer2.Add(self.OUTPUT, 1, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
#### Button to check the value of the global var variable and make sure
#### that the code in InputUpdate actually changes the value of the global
#### variable var
self.button = wx.Button(self, wx.ID_ANY, label='Var Value')
bSizer2.Add(self.button, 1, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
self.SetSizer(bSizer2)
self.button.Bind(wx.EVT_BUTTON, self.varValue)
def varValue(self, event):
#### Print the value of the global variable var
print(var)
if __name__ == '__main__':
app = wx.App()
frmA = FrameA(None)
frmB = FrameB(None)
frmA.Centre()
frmB.Centre()
frmA.Show()
frmB.Show()
app.MainLoop()
可供该模块中的所有类访问。如果将代码分成两个文件,则将有两个模块,apiVersion: v1
kind: Secret
metadata:
namespace: test
name: api-access-secret
type: Opaque
data:
username: YWRtaW4=
password: cGFzc3dvcmQ=
---
# Service account for preventing API access
apiVersion: v1
kind: ServiceAccount
metadata:
namespace: test
name: no-access-sa
---
# Service account for accessing secrets API
apiVersion: v1
kind: ServiceAccount
metadata:
namespace: test
name: secret-access-sa
---
# A role with no access
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: test
name: no-access-cr
rules:
- apiGroups: [""] # "" indicates the core API group
resources: [""]
verbs: [""]
---
# A role for reading/listing secrets
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: test
name: secret-access-cr
rules:
- apiGroups: [""] # "" indicates the core API group
resources: ["secrets", "pods"]
verbs: ["get", "watch", "list"]
---
# The role binding to combine the no-access service account and role
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
namespace: test
name: no-access-rb
subjects:
- kind: ServiceAccount
name: no-access-sa
roleRef:
kind: Role
name: no-access-cr
apiGroup: rbac.authorization.k8s.io
---
# The role binding to combine the secret-access service account and role
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
namespace: test
name: secret-access-rb
subjects:
- kind: ServiceAccount
name: secret-access-sa
roleRef:
kind: Role
name: secret-access-cr
apiGroup: rbac.authorization.k8s.io
---
# Create a pod with the no-access service account
kind: Pod
apiVersion: v1
metadata:
namespace: test
name: no-access-pod
spec:
serviceAccountName: no-access-sa
containers:
- name: no-access-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c" ]
args:
- while true; do
env;
done
env:
# Define the environment variable
- name: SPECIAL_LEVEL_KEY
valueFrom:
secretKeyRef:
name: api-access-secret
key: username
---
# Create a pod with the secret-access service account
kind: Pod
apiVersion: v1
metadata:
namespace: test
name: secret-access-pod
spec:
serviceAccountName: secret-access-sa
containers:
- name: access-container
image: k8s.gcr.io/busybox
command: [ "/bin/sh", "-c" ]
args:
- while true; do
env;
done
env:
# Define the environment variable
- name: SPECIAL_LEVEL_KEY
valueFrom:
secretKeyRef:
name: api-access-secret
key: username
仅在定义它的模块中可以访问,除非您也将定义var的模块导入另一个模块中。
带有一些相关注释的代码:
SPECIAL_LEVEL_KEY=admin