Python 3 Tkinter - How to call a function from another class - python-2.7

I am trying to get my save button to call a function from another class. I would like to click on the save button as much as I want and it should print "hello people" every time. Though, I am having trouble in getting the save button to work.
import tkinter as tk
from tkinter import filedialog
class Application(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.pack()
self.createWidgets()
def createWidgets(self):
#save button
self.saveLabel = tk.Label(self.parent, text="Save File", padx=10, pady=10)
self.saveLabel.pack()
#When I click the button save, I would like it to call the test function in the documentMaker class
self.saveButton = tk.Button(self.parent, text = "Save", command = documentMaker.test(self))
self.saveButton.pack()
class documentMaker():
def test(self):
print ("hello people")
root = tk.Tk()
app = Application(root)
app.master.title('Sample application')
object = documentMaker()
object.test()
app.mainloop()

In your documentMaker class, change the test method to a #staticmethod:
class documentMaker():
#staticmethod
def test(cls):
print ("hello people")
Then your saveButton's command can be:
command = documentMaker.test
A staticmethod is bound to the class, not to an instance of the class like an instance method. So, we can call it from the class's name directly. If you did not want it to be a staticmethod, you could keep it an instance method and have the command line change to:
command = documentMaker().test

Related

Print Message in Text Widget that is part of a class

I am fairly new to object-oriented programming. I have a class called MessageWindow which generates a Tkinter text widget inside of a Frame. In my main function I want to write a message into the text widget but when I run the program I get: Class MessageWindow has no attribute text1
I've tried following other responses here on Stack overflow, and another tutorial on the web and I always end up back at the same error
from Tkinter import *
class MessageWindow(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.pack(fill='both', expand='yes')
self.text1 =Text(self,height=9, width=13)
self.text1.pack()
def main():
root=Tk()
root.title("MessageWindow")
app = MessageWindow(root)
root.mainloop()
MessageWindow.text1.insert(END,"This is a test Message")
MessageWindow.text1.pack()
if __name__=='__main__':
main()
Any help would be appreciated. I need to understand this for a larger GUI project that I am working on.
Ok so
1. You are referring to MessageWindow as your class object when you should be referring to app, as you have instantiated the class as 'app' in your main definition.
2. Your root.mainloop() should be after all your GUI code as it will not take affect if it is after/outisde the mainloop.
So this is the How the code is supposed to be:
from Tkinter import *
class MessageWindow(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.pack(fill='both', expand='yes')
self.text1 = Text(self,height=9, width=13)
self.text1.pack()
def main():
root=Tk()
root.title("MessageWindow")
app = MessageWindow(root)
# Fix one
app.text1.insert(END,"This is a test Message")
app.text1.pack()
# Fix two
root.mainloop()
if __name__=='__main__':
main()

PyQt 4: Get Position of Toolbar

Hy guys,
in my executable program there is a toolbar. Well, the user decides to move the toolbar. Now the toolbar is floating. I know I have to conntect the floating-signals that is emittted when the toolbar ist arranged by the user. How can I save the new position of the toolbar? I know the method of adding the toolbar to the main window with a position:self.addToolBar( Qt.LeftToolBarArea , toolbar_name). In the handle_floating()-method you see what I want: There I want to get the position currently, but how? You also see I have just added one member variable, named self.toolbar_pos, to hold the position of the toolbar. My idea is, when application is terminated I want to serialize this value to a file, and later, when application is ran again its will read that file and set the toolbar accordingly. But this is no problem. Currently I don't have no idea to get the position of the toolbar.
I need your help :)
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore
class Example(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.toolbar_pos = None
self.initUI()
def initUI(self):
exitAction = QtGui.QAction(QtGui.QIcon('exit24.png'), 'Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.triggered.connect(QtGui.qApp.quit)
self.toolbar = QtGui.QToolBar(self)
self.toolbar.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon)
self.addToolBar(self.toolbar )
self.toolbar.addAction(exitAction)
self.toolbar.setAllowedAreas(QtCore.Qt.TopToolBarArea
| QtCore.Qt.BottomToolBarArea
| QtCore.Qt.LeftToolBarArea
| QtCore.Qt.RightToolBarArea)
self.addToolBar( QtCore.Qt.LeftToolBarArea , self.toolbar )
self.toolbar.topLevelChanged.connect(self.handle_floating)
def handle_floating(self, event):
# The topLevel parameter is true
# if the toolbar is now floating
if not event:
# If the toolbar no longer floats,
# then calculate the position where the
# toolbar is located currently.
self.toolbar_pos = None
print "Get position: ?"
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
ex.setGeometry(300, 300, 300, 200)
ex.setWindowTitle('Toolbar example')
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
The QMainWindow class already has APIs for this: i.e. saveState and restoreState. These can be used to save and restore the state of all the toolbars and dock-widgets in your application.
To use them, you first need to make sure that all your toolbars and dock-widgets are given a unique object-name when they are created:
class Example(QtGui.QMainWindow):
...
def initUI(self):
...
self.toolbar = QtGui.QToolBar(self)
self.toolbar.setObjectName('foobar')
Then you can override closeEvent to save the state:
class Example(QtGui.QMainWindow):
...
def closeEvent(self, event):
with open('/tmp/test.conf', 'wb') as stream:
stream.write(self.saveState().data())
(NB: I've just used a temporary file here for testing, but it would obviously be much better to use something like QSettings in your real application).
Finally, you can restore the state that was saved previously:
class Example(QtGui.QMainWindow):
def __init__(self, parent=None):
...
self.initUI()
try:
with open('/tmp/test.conf', 'rb') as stream:
self.restoreState(QtCore.QByteArray(stream.read()))
except IOError:
pass

Multiple Windows in Tkinter (Python 2.7)

So, for an assignment that I am supposed to do for my programming course, I ran into a bit of an issue. We never really went through how to create child windows // secondary windows, and for some reason the things I add in the first window are not showing up?...
So far my code looks like:
from Tkinter import*
class Window(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
self.master = master
self.button1 = Button(self, text="Create new window", command=self.New_Window)
self.button1.pack(fill=BOTH)
def New_Window(self):
win = Toplevel(self)
win.title("New Window")
etiquette1 = Label(root, text = "Text shenanigans")
etiquette1.pack()
if __name__ == "__main__":
root = Tk()
main = Window(root)
main.mainloop()
Also, another question is how can I name the first window? It's just called "tk" by default, but if I try to do something like the example below, it does not work.
self.__init__.title("Main Window")
The first argument when creating widgets is the "parent" -- the widget into which the new widget goes. If you want a label to be in the Toplevel, make the Toplevel be the parent:
etiquette1 = Label(win, ...)
To set the title of the window, call the title method:
root.title("This is the root window")
...
win.title("This is the second window")

Invoking destroy() method on window after mainloop in tkinter

I'm relatively new to Python and Tkinter and I am striving to get my head over how mainloop and the after method work.
More specifically, I want to create a splashScreen, which goes away after a timeframe, and then the actual mainWindow is shown.
My code looks like this:
class SplashScreen:
# some code here
def destroyMe(self):
self.destroy()
def destroySplashScreen(self, timeToSleep=0):
if timeToSleep > 0:
self.master.after(timeToSleep*1000, self.destroyMe())
# some other code here
if __name__ == '__main__':
root = Toplevel()
mySP = SplashScreen(root)
mySP.populateSplashScreen()
mySP.destroySplashScreen(5)
root.mainloop()
However, what the code does is to create the whole window after the timeframe given (5 sec.) without any content. Instead, it should create it, wait 5 sec. and then destroy it.
Working example
after expects only function name (without ()).
destroy() needs self.master
from Tkinter import *
class SplashScreen:
# some code here
def __init__(self, master):
self.master = master
self.master.title("SplashScreen")
def destroyMe(self):
self.master.destroy()
def destroySplashScreen(self, timeToSleep=0):
if timeToSleep > 0:
self.master.after(timeToSleep*1000, self.destroyMe)
# some other code here
if __name__ == '__main__':
root = Toplevel()
mySP = SplashScreen(root)
#mySP.populateSplashScreen()
mySP.destroySplashScreen(3)
root.mainloop()
BTW: Toplevel is used to create "child" window so (in example) it create automaticly "parent" window - so I add title("SplashScreen")
BTW: if you will use command= in widget - it also expects function name without ().
If you use (in command or after) function with () you run that function and result is assigned to command= or use as parameter for after.

Python Tkinter instance has no attribute 'tk'

AttributeError: MyGUI instance has no attribute 'tk'
Also, how do I make the created window have a fixed size and not be able to resize with the mouse? Or after changing label value by clicking on button.
My code:
from Tkinter import*
class MyGUI(Frame):
def __init__(self):
self.__mainWindow = Tk()
#lbl
self.labelText = 'label message'
self.depositLabel = Label(self.__mainWindow, text = self.labelText)
#buttons
self.hi_there = Button(self.__mainWindow)
self.hi_there["text"] = "Hello",
self.hi_there["command"] = self.testeo
self.QUIT = Button(self.__mainWindow)
self.QUIT["text"] = "QUIT"
self.QUIT["fg"] = "red"
self.QUIT["command"] = self.quit
#place on view
self.depositLabel.pack()
self.hi_there.pack() #placed in order!
self.QUIT.pack()
#What does it do?
mainloop()
def testeo(self):
self.depositLabel['text'] = 'c2value'
print "testeo"
def depositCallBack(self,event):
self.labelText = 'change the value'
print(self.labelText)
self.depositLabel['text'] = 'change the value'
myGUI = MyGUI()
What's wrong?
Thanks
You should invoke the super constructor for Frame. Not sure, but I guess this will set the tk attribute that the quit command relies on. After that, there's no need to create your own Tk() instance.
def __init__(self):
Frame.__init__(self)
# self.__mainWindow = Tk()
Of course, you will also have to change the constructor calls for your widgets accordingly, e.g.,
self.hi_there = Button(self) # self, not self.__mainWindow
or better (or at least shorter): set all the attributes directly in the constructors:
self.hi_there = Button(self, text="Hello", command=self.testeo)
Also add self.pack() to your constructor.
(Alternatively, you could change the quit command to self.__mainWindow.quit, but I think the above is better style for creating Frames, see e.g. here.)
This error typically means you are calling SomeTKSuperClass.__init__ and forgetting the first parameter, which must be self. Remember that __init__ is a class method (static function) in this context, not an instance method, which means that you must explicitly pass it self.