def home(self):
btn = QtGui.QPushButton("Log in", self)
self.show()
if btn.clicked:
btn.clicked.connect(btn.deleteLater)
self.Page()
else:
pass
def Page(self):
btn2 = QtGui.QPushButton("Exit", self)
self.show()
Sorry if the indenting isn't correct here, but it is in my python file:
So the btn does delete when it is pressed, but Page function isn't correctly being run because the btn2 doesn't appear.
This is only the relevant code snippet pasted.
TIA for help as to why the Page function isn't being run. I am using python 2,7 and pyqt4
It looks like btn variable is local inside home method. This means it is only visible within this method (unless it's defined outside in a higher-level scope.)
If you want to share a variable in multiple methods of the class, you should store it as an object property - that's why you'll need some OOP. For example (assuming the made-up rest of your class definition):
class YourApp(object):
def __init__(self):
# All the preparations should go here.
# If self.btn is created later dynamically,
# it's still recommended to declare it here
# and assign `None` to it
self.btn = QtGui.QPushButton("Log in", self)
def home(self):
# Do stuff with self.btn
self.btn.spam()
pass
def page(self):
# Do other stuff with self.btn
self.btn.eggs()
Related
I have an auto generated code which generates a GUI that has various widgets in it. One of the widget is a ScrolledListBox. A part of the code is shown below:
class New_Toplevel_1:
def __init__(self, top=None):
self.Scrolledlistbox4.configure(background="white")
self.Scrolledlistbox4.configure(font="TkFixedFont")
self.Scrolledlistbox4.configure(highlightcolor="#d9d9d9")
self.Scrolledlistbox4.configure(selectbackground="#c4c4c4")
self.Scrolledlistbox4.configure(width=10)
I want to access the Scrolledlistbox4 from outside this class. So for example, I would like to write to write a function that updates the ScrolledListBox whenever I call it. I am relatively new to python and would like to know how can I accomplish this.
You need to first create a Scrolledlistbox4 object as an attribute:
self.scrolled_listbox = Scrolledlistbox4(...)
then you can do all configures in outermost scope like:
a = New_Toplevel_1()
a.scrolled_listbox.configure(background='white')
...
In below example "Outside Button" changes the text option of a class' button from the outside:
import tkinter as tk
class FrameWithButton(tk.Frame):
def __init__(self, master):
super().__init__(master)
self.btn = tk.Button(root, text="Button")
self.btn.pack()
root = tk.Tk()
an_instance = FrameWithButton(root)
an_instance.pack()
def update_button():
global an_instance
an_instance.btn['text'] = "Button Text Updated!"
tk.Button(root, text="Outside Button", command=update_button).pack()
root.mainloop()
Goal of the script:
(3) different windows, each in its own class, with its own widgets and layout, are created via Toplevel and callbacks.
When a new (Toplevel) window is created, the previous one is destroyed. Thus, only one window is visible and active at a time.
Problem?
Basically, I've tried many things and failed, so I must understand too little of ["parent", "master", "root", "app", "..."] :(
Note on raising windows:
I have implemented a successful example of loading all frames on top of each other, and controlling their visibility via the .raise method.
For this problem, however, I don't want to load all the frames at once.
This is an abstracted version of a quiz program that will require quite a lot of frames with images, which makes me reluctant to load everything at once.
Script (not working; bugged):
#!/usr/bin/env python
from Tkinter import *
import tkMessageBox, tkFont, random, ttk
class First_Window(Frame):
"""The option menu which is shown at startup"""
def __init__(self, master):
Frame.__init__(self, master)
self.gotosecond = Button(text = "Start", command = self.goto_Second)
self.gotosecond.grid(row = 2, column = 3, sticky = W+E)
def goto_Second(self):
self.master.withdraw()
self.master.update_idletasks()
Second_Window = Toplevel(self)
class Second_Window(Toplevel):
"""The gamewindow with questions, timer and entrywidget"""
def __init__(self, *args):
Toplevel.__init__(self)
self.focus_set()
self.gotothird = Button(text = "gameover", command = self.goto_Third)
self.gotothird.grid(row = 2, column = 3, sticky = W+E)
def goto_Third(self):
Third_Window = Toplevel(self)
self.destroy()
class Third_Window(Toplevel):
"""Highscores are shown with buttons to Startmenu"""
def __init__(self, *args):
Toplevel.__init__(self)
self.focus_set()
self.master = First_Window
self.gotofirst = Button(text = "startover", command = self.goto_First)
self.gotofirst.grid(row = 2, column = 3, sticky = W+E)
def goto_First(self):
self.master.update()
self.master.deiconify()
self.destroy()
def main():
root = Tk()
root.title("Algebra game by PJK")
app = First_Window(root)
root.resizable(FALSE,FALSE)
app.mainloop()
main()
The problem is not really a Tkinter problem, but a basic problem with classes vs. instances. Actually, two similar but separate problems. You probably need to read through a tutorial on classes, like the one in the official Python tutorial.
First:
self.master = First_Window
First_Window is a class. You have an instance of that class (in the global variable named app), which represents the first window on the screen. You can call update and deiconify and so forth on that instance, because it represents that window. But First_Window itself isn't representing any particular window, it's just a class, a factory for creating instances that represent particular windows. So you can't call update or deiconify on the class.
What you probably want to do is pass the first window down through the chain of windows. (You could, alternatively, access the global, or do various other things, but this seems cleanest.) You're already trying to pass it to Second_Window, you just need to stash it and pass it again in the Second_Window (instead of passing self instance, which is useless—it's just a destroyed window object), and then stash it and use it in the Third_Window.
Second:
Second_Window = Toplevel(self)
Instead of creating an instance of the Second_Window class, you're just creating an instance of the generic Toplevel class, and giving it the local name Second_Window (which temporarily hides the class name… but since you never use that class, that doesn't really matter).
And you have the same problem when you try to create the third window.
So:
class First_Window(Frame):
# ...
def goto_Second(self):
# ...
second = Second_Window(self)
class Second_Window(Toplevel):
def __init__(self, first, *args):
Toplevel.__init__(self)
self.first = first
# ...
def goto_Third(self):
third = Third_Window(self.first)
self.destroy()
class Third_Window(Toplevel):
"""Highscores are shown with buttons to Startmenu"""
def __init__(self, first, *args):
Toplevel.__init__(self)
self.first = first
# ...
def goto_First(self):
self.first.update()
self.first.deiconify()
self.destroy()
This is one of those questions that is really frustrating for me to ask because I'm sure the answer is out there, I just haven't been able to word the searches correctly. Basically I am new to GUI programming (done a decent amount of embedded C/C++) and learning wxPython to start out.
I'm making an app to read and write to a config file. So I have a StaticText to display the name of the parameter to be read/written, a TextCtrl to display the value and allow user input, and then a Get button and a Set button. I'll call all of these together a "group" of widgets. For this app obviously this group will be repeated several times. Rather than write and maintain all that code by hand, I thought it would be easier to simply have a list of the config parameters I want to be able to edit, and then iterate through the list and generate an instance of this "group" of widgets for each item in the list. I made it work except for one thing: I had to bind all of the Get buttons to the same function. Same thing with the Set buttons. Is there any way from within those functions to know which Get or Set button was pushed, and thus which parameter to find and edit in the config file? I'm sure there's a way to do this with parent or IDs or something but I'm just too new to OOP.
I assume that the get button reads the parameter value from the config file and displays the value.
Why do you need one get button for each parameter? I would have just one get button for tham all. When the user clicks the get button, every parameter is read from the file and all the displays are updated.
A similar approach for the set button. One set button for them all - when the button is pressed every parameter that has a new value entered is updated in the config file. Parameters where the user has not entered a new value remain with their previous values.
This scheme is easier to code, and easier for the user also.
However, I really suggest you look at the wxPropertyGrid widget. This has the potential to make your life a lot easier! Here's a screenshot showing one in action
In your button's event handler, you can do something like this:
btn = event.GetEventObject()
btn.GetId()
btn.GetName()
Then you just use an If statement to decide what to do based on whatever info you want to use. Note: you can set the button's name when you create the button like this:
setBtn = wx.Button(self, label="Set", name="SetX")
You might find this article on wxPython and ConfigObj helpful too: http://www.blog.pythonlibrary.org/2010/01/17/configobj-wxpython-geek-happiness/
You can derive your own class from the wx.Button and add one or more attributes to it to make the button remember any information you want.
You can use this stored information during a callback function call. Something like:
import wx
L = [("1", "One"), ("2", "Two"), ("3", "Three")]
# =====================================================================
class MemoryButton(wx.Button):
def __init__(self, memory, *args, **kwargs):
wx.Button.__init__(self, *args, **kwargs)
self.memory = memory
# =====================================================================
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = wx.Panel(self)
self.buttons = []
for description in L:
button = MemoryButton(memory=description[1], parent=self.panel,
label=description[0])
button.Bind(wx.EVT_BUTTON, self.OnMemoryButton)
self.buttons.append(button)
self.sizer = wx.BoxSizer()
for button in self.buttons:
self.sizer.Add(button)
self.panel.SetSizerAndFit(self.sizer)
self.Show()
# -----------------------------------------------------------------
def OnMemoryButton(self, e):
print("Clicked '%s'" % e.GetEventObject().memory)
# =====================================================================
app = wx.App(False)
win = MainWindow(None)
app.MainLoop()
or alternatively:
import wx
L = [("1", "One"), ("2", "Two"), ("3", "Three")]
# =====================================================================
class MemoryButton(wx.Button):
def __init__(self, memory, *args, **kwargs):
wx.Button.__init__(self, *args, **kwargs)
self.memory = memory
def OnButton(self, e):
print("Clicked '%s'" % self.memory)
# =====================================================================
class MainWindow(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = wx.Panel(self)
self.buttons = []
for description in L:
button = MemoryButton(memory=description[1], parent=self.panel,
label=description[0])
button.Bind(wx.EVT_BUTTON, button.OnButton)
self.buttons.append(button)
self.sizer = wx.BoxSizer()
for button in self.buttons:
self.sizer.Add(button)
self.panel.SetSizerAndFit(self.sizer)
self.Show()
# =====================================================================
app = wx.App(False)
win = MainWindow(None)
app.MainLoop()
Is there a way, from a button to reinitialize a specific panel? I have a section of an app that looks for specific files in the OS and then creates some checkboxes and textctrls and then adds them to the sizer dynamically:
for db in numDB:
if xIndex >= 12:
pass
xIndex += 1
else:
check = wx.CheckBox(self, -1, db)
sizer.Add(check, pos=(xIndex,0), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=10)
label = wx.StaticText(panel, label="")
sizer.Add(label, pos=(xIndex,1), flag=wx.LEFT|wx.ALIGN_CENTER_VERTICAL, border=10)
name = wx.TextCtrl(panel)
#Set Temp Name
if db.endswith('.db'):
name.Value = db[:-3]
sizer.Add(name, pos=(xIndex,2), span=(1,3),flag=wx.EXPAND|wx.RIGHT|wx.ALIGN_CENTER_VERTICAL, border=10)
xIndex +=1
I have a refresh button, in the event the user adds a new db, or removes one. The simplest concept for me, at least, is to try to find a way to rerun the init.
I'm trying to reference the init by:
def onRefreshClick(self, event):
self.__init__
That def happens within the Class itself, but it doesn't seem to do anything. And I'm super new to the whole wxPython and Python in general, so it's probably something simple that I cannot quite figure out.
Edited to try and incorporate super():
class LaunchPanel(wx.Panel):
"""
Launch Tab for finding and launching .db files
"""
#----------------------------------------------------------------------
def __init__(self, parent):
""""""
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
super (LaunchPanel)
self.initialize()
def initialize(self):
panel = self
sizer = wx.GridBagSizer(11, 3)
<snip...>
This results in my panel being empty. When I hit refresh, it adds (I think) all my elements to the upper left portion of the panel...? I'm sure it's easy, whatever I am doing wrong...?
You're missing parentheses:
self.__init__() # now, this is a method call
A simple solution is to have your __init__ do nothing but call another method, then you can call that method whenever you want:
def __init__(self, ...):
super(...)
self.initialize()
def initialize(self):
<actual intialization code here>
I think that's a lot cleaner than directly calling __init__. Plus, this lets you separate stuff that must truly be done only at object instantiation (such as calling the init of the superclass) from the code you want to call multiple times.
So I've been creating my GUI with Qt for my Python application. I've now come to a situation where after a button has been pushed the appropriate deferred gets executed, we perform some tasks then I need to open up a separate window that contains one or two things. But I can't seem to figure out how to create this new separate window. Could anyone give me an example of how to create one?
A common error that can drive you crazy is forgetting to store the handle of the popup window you create in some python variable that will remain alive (e.g. in a data member of the main window).
The following is a simple program that creates a main window with a button where pressing the button opens a popup
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import sys
from PyQt4.Qt import *
class MyPopup(QWidget):
def __init__(self):
QWidget.__init__(self)
def paintEvent(self, e):
dc = QPainter(self)
dc.drawLine(0, 0, 100, 100)
dc.drawLine(100, 0, 0, 100)
class MainWindow(QMainWindow):
def __init__(self, *args):
QMainWindow.__init__(self, *args)
self.cw = QWidget(self)
self.setCentralWidget(self.cw)
self.btn1 = QPushButton("Click me", self.cw)
self.btn1.setGeometry(QRect(0, 0, 100, 30))
self.connect(self.btn1, SIGNAL("clicked()"), self.doit)
self.w = None
def doit(self):
print "Opening a new popup window..."
self.w = MyPopup()
self.w.setGeometry(QRect(100, 100, 400, 200))
self.w.show()
class App(QApplication):
def __init__(self, *args):
QApplication.__init__(self, *args)
self.main = MainWindow()
self.connect(self, SIGNAL("lastWindowClosed()"), self.byebye )
self.main.show()
def byebye( self ):
self.exit(0)
def main(args):
global app
app = App(args)
app.exec_()
if __name__ == "__main__":
main(sys.argv)
What I think can be surprising for Python users and may be is the problem you are facing is the fact that if you don't store a reference to the new widget in the main e.g. by using w = MyPopup(...) instead of self.w = MyPopup(...) the window apparently doesn't appear (actually it's created and it's immediately destroyed).
The reason is that when the local variable w goes out of scope as no one is explicitly referencing the widget the widget gets deleted. This can be seen clearly because if you press again the button you'll see that as the second popup appears the first one is closed.
This also means that if you need to create several popups you have for example to put them in a python list and you should remove them from this list once the popups are closed by the user. The equivalent in the example could be changing to self.w = [] in constructor and then doing self.w.append(MyPopup(...)). Doing that would allow you to open several popups.
Generally, you just show multiple parentless windows with someQWidget.show(), like:
w1 = QLabel("Window 1")
w2 = QLabel("Window 2")
w1.show()
w2.show()
But most likely, you want a modal standard Dialog like this. Also be sure to understand modal dialogs.