Hi guys!
I started learning Python. I am trying to make a simple program which has the visual creation in 1 class and the functions in another class. I get the error: Traceback (most recent call last): File "E:\Other\OS_Python_Testing\Temporary.py", line 135, in OnPlus Text = VisualCreation.TextControl.GetValue()#VisualCreation.sc.GetValue() AttributeError: 'CommandEvent' object has no attribute 'TextControl'
The idea is how to use the function from the other class(Functions).
I started learning Python. I am trying to make a simple program which has the visual creation in 1 class and the functions in another class. I get the error: Traceback (most recent call last): File "E:\Other\OS_Python_Testing\Temporary.py", line 135, in OnPlus Text = VisualCreation.TextControl.GetValue()#VisualCreation.sc.GetValue() AttributeError: 'CommandEvent' object has no attribute 'TextControl'
The idea is how to use the function from the other class(Functions).
Code:
#!/usr/bin/env python3 #Settings.
# -*- coding: utf-8 -*- #Encoding of the source code.
# simple.py #Name of the file.
import wx
import wx.adv
#Global variables
APP_EXIT = 1#ID
APP_SECOND = 2#ID
Text = 'No last action.'#The variable is used to return a result as char or int
class Example(wx.Frame):#"wx.VisualCreation" is turned into a class. Initialization for every class.
def __init__(VisualCreation, *args, **kwargs):#???Initialization???
super(Example, VisualCreation).__init__(*args, **kwargs)
VisualCreation.InitUI()
def InitUI(VisualCreation):#Main features for every class.
VisualCreation.locale = wx.Locale(wx.LANGUAGE_ENGLISH)#The locale is needed to avoid Python 3 bugs.
VisualCreation.SetSize((330, 250))#Set the program size. For windows 10 it needs to be "340, 250".
VisualCreation.SetTitle('Name')#Set the program title in the upest bar.
VisualCreation.Centre()#The program will start in the center of the screen.
#Menu bar visual creation block
menubar = wx.MenuBar()
help = wx.Menu()
help.Append(wx.ID_ANY, '&About')
help.Bind(wx.EVT_MENU, VisualCreation.OnAboutBox)
menubar.Append(help, '&Help')
VisualCreation.SetMenuBar(menubar)
Panel = wx.Panel(VisualCreation, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.TAB_TRAVERSAL, name='Panel')#Block visual creation for the panel for buttons and static text boxes. All of them must be in a panel.
VisualCreation.TextControl = wx.TextCtrl(parent=Panel, id=wx.ID_ANY, value='message', pos=(10, 145), size=(140, 20), style=wx.OK)
VisualCreation.TextControl1 = wx.TextCtrl(parent=Panel, id=wx.ID_ANY, value='message', pos=(170, 145), size=(140, 20), style=wx.OK)
####################################################################################
font = wx.Font(13, wx.DEFAULT, wx.NORMAL, wx.NORMAL)#size, family, style, weight/size, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL
st1 = wx.StaticText(Panel, wx.ID_ANY, label='Made by.', pos=(10, 175), style=wx.ALIGN_CENTRE)#Wx.StaticText(parent, id, label, position, size, style)
st1.SetFont(font)
#Buttons block visual creation
ButtonBinaryToAll = wx.Button(Panel, wx.ID_ANY, 'BinaryToAll', (10, 35))#Define the button BinToAll.
#Buttons block function bind
VisualCreation.Bind(wx.EVT_BUTTON, Functions.OnBinaryToAll, id=ButtonBinaryToAll.GetId())#Assign a function to the button +.
####################################################################################
####################################################################################
#Buttons block visual creation
ButtonDivision = wx.Button(Panel, wx.ID_ANY, '/', (235, 10))#Define the button +.
#Buttons block function bind
VisualCreation.Bind(wx.EVT_BUTTON, Functions.OnPlus, id=ButtonDivision.GetId())#Assign a function to the button +.
####################################################################################
####################################################################################
ButtonMultiplication = wx.Button(Panel, wx.ID_ANY, '*', (160, 10))#Define the button +.
#Buttons block function bind
VisualCreation.Bind(wx.EVT_BUTTON, Functions.OnMinus, id=ButtonMultiplication.GetId())#Assign a function to the button +.
#Program size/title/position block.
####################################################################################
####################################################################################
ButtonMinus = wx.Button(Panel, wx.ID_ANY, '-', (85, 10))#Define the button +.
#Buttons block function bind
VisualCreation.Bind(wx.EVT_BUTTON, Functions.OnMinus, id=ButtonMinus.GetId())#Assign a function to the button +.
####################################################################################
####################################################################################
#Buttons block visual creation
ButtonPlus = wx.Button(Panel, 1, '+', (10, 10))#Define the button +.
#Buttons block function bind
ButtonPlus.Bind(wx.EVT_BUTTON, Functions.OnPlus, id=ButtonPlus.GetId())#Assign a function to the button +.
####################################################################################
#If I use this option it works.
def OnPlus(VisualCreation, e):#Function for buttons +.
Text = VisualCreation.TextControl.GetValue()#VisualCreation.sc.GetValue()
Text1 = VisualCreation.TextControl1.GetValue()#VisualCreation.sc.GetValue()
Text2 = int(Text) + int(Text1)
dial = wx.MessageBox( str(Text2), 'Result:', wx.OK | wx.ICON_NONE)
def OnAboutBox(VisualCreation, e):
description = """Description.
"""
licence = """GNU GPL v3 license.
Warranty."""
info = wx.adv.AboutDialogInfo()
info.SetIcon(wx.Icon('logo.png', wx.BITMAP_TYPE_PNG))
info.SetName('Name')
info.SetVersion('1.0')
info.SetDescription(description)
info.SetCopyright('(C) 2019 www..com')
info.SetWebSite('http://www..com')
info.SetLicence(licence)
info.AddDeveloper('.com')
info.AddDocWriter('.com')
info.AddArtist('The .com team')
info.AddTranslator('.com')
wx.adv.AboutBox(info)
#Function block implemented in a class
class Functions():
#Does not works. Error: Traceback (most recent call last): File "E:\Other\OS_Python_Testing\Temporary.py", line 135, in OnPlus Text = VisualCreation.TextControl.GetValue()#VisualCreation.sc.GetValue() AttributeError: 'CommandEvent' object has no attribute 'TextControl'
def OnPlus(VisualCreation):#Function for buttons +.
Text = VisualCreation.TextControl.GetValue()#VisualCreation.sc.GetValue()
Text1 = VisualCreation.TextControl1.GetValue()#VisualCreation.sc.GetValue()
Text2 = int(Text) + int(Text1)
dial = wx.MessageBox( str(Text2), 'Result:', wx.OK | wx.ICON_NONE)
def main():
app = wx.App()
ex = Example(None)
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()