Dynamic Page creation tkinter - user-interface

I am following an example of Python code at this link. My ultimate goal is to try to create a dynamic number of pages using a database query. The query would return a list of items and I could create a dictionary entry instantiating each one as a page.
I attempted to create that here by creating a Page class which is instantiated in the dictionary with just a list of numbers, however I can't seem to get the StartPage showing, all that shows up is one of the Page pages. My source code:
class SeaofBTCapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
database_pages = [1, 2, 3, 4]
count = 0
for p in database_pages:
page_num = p
frame = Page(container, self)
self.frames[page_num] = frame
frame.grid(row=0, column=0, sticky="nsew")
count += 1
self.frames[count] = StartPage(container, self)
self.show_frame(count)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class Page(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="label for Page")
label.pack()
I tried adding it as a dictionary entry and calling that key. I get no errors, but the StartPage isn't visible. How can I show StartPage at the start? Is there a better way to achieve this?
EDIT
As suggested, I changed:
self.frames[count] = StartPage(container, self)
self.show_frame(count)
to:
frame = StartPage(container, self)
self.frames[count + 1] = frame
frame.grid(row=0, column=0, sticky='nsew')
self.show_frame(count + 1)
Thank you for the help!

You didn't call grid after creating StartPage, so it will be invisible. Add the following after you create StartPage:
start.grid(row=0, column=0, sticky="nsew")
You have another problem in that you are putting StartPage at index 4, but you already have a frame at index 4. That's not contributing to the problem in this question, but it will cause problems when you try to see the other page.

Related

Image will not display in tkinter [duplicate]

This code works:
import tkinter
root = tkinter.Tk()
canvas = tkinter.Canvas(root)
canvas.grid(row = 0, column = 0)
photo = tkinter.PhotoImage(file = './test.gif')
canvas.create_image(0, 0, image=photo)
root.mainloop()
It shows me the image.
Now, this code compiles but it doesn't show me the image, and I don't know why, because it's the same code, in a class:
import tkinter
class Test:
def __init__(self, master):
canvas = tkinter.Canvas(master)
canvas.grid(row = 0, column = 0)
photo = tkinter.PhotoImage(file = './test.gif')
canvas.create_image(0, 0, image=photo)
root = tkinter.Tk()
test = Test(root)
root.mainloop()
The variable photo is a local variable which gets garbage collected after the class is instantiated. Save a reference to the photo, for example:
self.photo = tkinter.PhotoImage(...)
If you do a Google search on "tkinter image doesn't display", the first result is this:
Why do my Tkinter images not appear? (The FAQ answer is currently not outdated)
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
def open_img():
global img
path = r"C:\.....\\"
img = ImageTk.PhotoImage(Image.open(path))
panel = Label(root, image=img)
panel.pack(side="bottom", fill="both")
but1 = Button(root, text="click to get the image", command=open_img)
but1.pack()
root.mainloop()
Just add global to the img definition and it will work
The problem is Python automatically deletes the references to the variable by a process known as Garbage Collection. The solution is to save the reference or to create a new reference.
The following are the ways:
Using self to increase the reference count and to save the reference.
import tkinter
class Test:
def __init__(self, master):
canvas = tkinter.Canvas(master)
canvas.grid(row = 0, column = 0)
self.photo = tkinter.PhotoImage(file = './test.gif') # Changes here
canvas.create_image(0, 0, image=self.photo) # Changes here
root = tkinter.Tk()
test = Test(root)
root.mainloop()
Saving it to a list to increase the reference count and to save the reference.
import tkinter
l=[]
class Test:
def __init__(self, master):
canvas = tkinter.Canvas(master)
canvas.grid(row = 0, column = 0)
photo = tkinter.PhotoImage(file = './test.gif')
l.append(photo)
canvas.create_image(0, 0, image=photo)
root = tkinter.Tk()
test = Test(root)
root.mainloop()
While using method 2, you can either make a global list as i did or use list inside the class. Both would work.
Some useful links:
About Garbage Collection 1
About Garbage Collection 2 (More useful)
As a rule of thumb, whenever you create your image in an indented block of code you need to safe a reference to that image. This is because of the python's automated garbage collection and it collects everything with a refcount of 0 when it destroys/leaves that frame/page/indented block of code.
The canonical way to deal with it is to have a list of images somewhere in the global namespace and add your image-references to that list. This is convenient but not very efficient and should be used for small applications.
import tkinter as tk
global_image_list = []
global_image_list.append(tk.PhotoImage(file = 'test.png'))
An more efficient way is to bound an attribute to your widget or class that holds that reference for you, as Bryan proposed in his answer. It doesn't make a difference if you do self.image or widget.image that was assigned widget = tk.Widget(.. before. But this also might not the right approach if you want to use that image further even when the widget is destroyed and garbage collected.
import tkinter as tk
root = tk.Tk()
label = tk.Label(root, text='test')
label.image = tk.PhotoImage(file = 'test.png')
label.configure(image=label.image)
Just add global photo as the first line inside the function.

How to get the size of the wxpython panel

I'm developing a calendar application
The top level window is a frame containing a panel that displays the calendar grid and a panel that contains a "Close" button.
I'm unable to obtain the size of the calendar grid panel.
When I add code to get the panel size, the result is (20,20), which cannot be correct
The screen size is (1920,1080) so I'm expecting something like (1920, 1000)
When I add the wx.lib.inspection module, I see the correct size being displayed. It is (1920, 968)
Can anyone shed some light how to get the correct size of the panel?
This is the code I have so far
import wx
class DrawFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent=None, title='Agenda', style= wx.CAPTION | wx.CLOSE_BOX)
self.drawpanel = DrawPanel(self)
self.buttonpanel = ButtonPanel(self)
self.framesizer = wx.BoxSizer(wx.VERTICAL)
self.framesizer.Add(self.drawpanel,1, flag=wx.EXPAND)
# Add an empty space 10 pixels high above and below the button panel
self.framesizer.Add((0,10),0)
self.framesizer.Add(self.buttonpanel,0, flag=wx.EXPAND)
self.framesizer.Add((0,10),0)
self.SetSizer(self.framesizer)
self.SetInitialSize()
self.Maximize()
self.Show()
def GetPanelSize(self):
return self.drawpanel.GetSize()
def OnClose(self, event):
self.Close()
class DrawPanel(wx.Panel):
# This panel's parent is DrawFrame. DrawFrame is the top level window.
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
self.parent = parent
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.x1, self.y1, self.x2, self.y2 = wx.GetClientDisplayRect()
b = self.x1, self.y1, self.x2, self.y2
print b
self.width, self.height = wx.GetDisplaySize()
c = self.width, self.height
print c
def OnPaint(self, event=None):
dc = wx.PaintDC(self)
dc.Clear()
dc.SetPen(wx.Pen(wx.BLACK, 2))
dc.SetBrush(wx.Brush('WHITE'))
"""
DrawRectangle (self, x, y, width, height)
Draw a rectangle with the given corner coordinate and size.
x and y specify the top left corner coordinates and both width and height are positive.
"""
dc.DrawRectangle(self.x1 + 5, self.y1, self.x2 - 10, self.y2 - 60)
dc.DrawLine(40, 100, 600, 100)
class ButtonPanel(wx.Panel):
# This panel's parent is DrawFrame. DrawFrame is the top level window.
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
self.parent=parent
self.buttonpanelsizer = wx.BoxSizer(wx.HORIZONTAL)
self.closebutton = wx.Button(self, label = 'Close')
self.Bind(wx.EVT_BUTTON, self.OnClose, self.closebutton)
self.buttonpanelsizer.AddStretchSpacer(prop=1)
self.buttonpanelsizer.Add(self.closebutton, 0, wx.ALIGN_CENTER)
self.SetSizer(self.buttonpanelsizer)
def OnClose(self, event):
self.parent.OnClose(event)
app = wx.App(False)
frame = DrawFrame()
print frame.GetPanelSize()
app.MainLoop()
Much appreciated,
Thanks
You are calling the GetPanelSize too early. Keep in mind that wxPython (and pretty much any GUI framework) is event based. That means that for it to work it must keep processing events, which in case of wxPython means that app.MainLoop() must run. So do not call GetPanelSize before calling app.MainLoop(). Instead, call it when you need it. Do you need it when you paint something? Just use dc.GetSize(). Do you need it elsewhere? Process the wx.EVT_SIZE event and store the current size. Possibly you will have to trigger some action in the EVT_SIZE handler.

Whole widget freezes/doesn't load when trying self.after() in Tkinter

I want to create a simple window showing a bit of text that is quickly changing between the three primary colours. When I try this code, the window doesn't load for a few seconds, then the text just appears red, rather than changing. What am I doing wrong? Thanks
from tkinter import *
PRIMARY = ("#FF0000", "#00FF00", "#0000FF")
class Multicolour(Frame):
def __init__(self, master):
super().__init__(master)
self.grid()
self.txt = Label(self,
text="Colour change")
self.txt.grid()
self.colour_index = 0
for i in range(3000):
self.after(10, self.change)
def change(self):
self.txt.configure(fg=PRIMARY[self.colour_index])
self.colour_index += 1
if self.colour_index > 2:
self.colour_index = 0
root = Tk()
app = Multicolour(root)
root.mainloop()

Function to set image of buttons causes image to jump between buttons

I'm trying to teach myself the tkinter module by programming minesweeper. I have created a grid with buttons and a method to set an image flag to cells. It works, in that when you press the right mouse button the image of the button changes as desired, but when you right click on the next button the image just moves to the next button, rather than creating a second flag. I want to be able to place a new flag image on each cell that I right click, rather than just shuffle the image around. Here's my code:
import tkinter as Tk
def main():
root = Tk.Tk()
root.geometry('{}x{}'.format(700, 700))
instance = Minesweeper(root, 10, 10)
root.mainloop()
class Minesweeper:
def __init__(self, parent, height, width):
self.top_frame = Tk.Frame(parent)
self.top_frame.place(anchor=Tk.CENTER, relx=0.5, rely=0.5)
self.frames = []
self.buttons = []
index = 0
for x in range(height):
for y in range(width):
self.frames.append(Tk.Frame(self.top_frame, height=50, width=50))
self.buttons.append(Tk.Button(self.frames[index], bg="white"))
self.frames[index].grid_propagate(False)
self.frames[index].columnconfigure(0, weight=1)
self.frames[index].rowconfigure(0, weight=1)
self.frames[index].grid(row=x, column=y)
self.buttons[index].grid(sticky="wens")
self.buttons[index].bind('<Button-3>', self.flag)
index += 1
def flag(self, event):
self.flag = Tk.PhotoImage(file="flag.png")
event.widget.configure(image=self.flag)
if __name__ == "__main__":
main()
Seems that the below fixed it:
def flag(self, event):
self.flag = Tk.PhotoImage(file="flag.png")
event.widget.image = self.flag # <---- this seemed to fix it
event.widget.configure(image=self.flag)

wxPython: binding a dynamically created spinner to an event

I used the code below to create several spinners with a for loop.
Now, I can't figure out how to bind an event so that I know which spinner is being modified so that I can put the spinner value into the right variable.
If I can figure out which spinner is calling the handler I could map it to the correct variable.
Any thoughts? Is this even possible?
Thanks in advance!
import wx
class spinnerFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent,id, "Spinner Frame", size = (300,200))
#constants
spnr_sz = (50,-1)
names = ('name1','name2','name3','name4','name5','name6')
sizer = wx.GridBagSizer(5, 5)
# TEXT FONT EXAMPLE
# m_text = wx.StaticText(panel, -1, "Hello World!")
# m_text.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
# m_text.SetSize(m_text.GetBestSize())
#temp
sizer = wx.GridBagSizer(5, 5)
row = 0
for n in names:
row += 1
my_label = wx.StaticText(self, -1, n)
spinner = wx.SpinCtrl(self, -1, size = spnr_sz, min = 0, initial = 10 )
self.Bind(wx.EVT_SPINCTRL, self.OnCompute, spinner)
sizer.Add(my_label, (row,0))
sizer.Add(spinner, (row,1))
sizer.AddGrowableRow(7)
sizer.AddGrowableCol(4)
self.SetSizerAndFit(sizer)
self.Centre()
def OnCompute(self,event):
# a = spinner.GetValue()
# ????
if __name__=='__main__':
app = wx.App(True) # was False
frame = wx.Frame(None)
frame = spinnerFrame (parent=None, id = -1)
frame.Show()
app.MainLoop()
Yes, this is possible. The easiest way I know is to associate your unique name with each call to Bind, either by giving the spinner a name or using partial functions (or lambdas, but lambdas can end up being messy), and then check for the name in the handler. Examples of how to use the name are given in this previous SO question.

Resources