0

My basic issue is trying to get my background to show.

It loads and shows when not including extra code but as soon as i try and run some other processes in the loop the background stops loading/showing

A bit of further background is that i've produced code to run a batch process for my degree project. When trying to now incorporate a GUI to this im trying Tkinter, bearing in mind this is all new to me so maybe i've missed a few points when searching for similar issues/rectifications.

As i'm trying to build the GUI learning as i go i've tried a very basic thing to build upon that looks like the following:

import time
from tkinter import *
import tkinter as Tk   ## notice lowercase 't' in tkinter here

root = Tk.Tk()
root.wm_geometry("1024x768")
root.title('Rpi Automated MicroBrewery')

a = StringVar()
a.set("1234")

text1 = StringVar()
text1.set("Header Full")

def startBrew():
    print('brew has started')

def a():
    print('a')
def b():
    print('b')
def c():
    print('c')

def main():
    a()
    b()
    c()

class Application(Tk.Frame):
    def __init__(self, master=None):
        Tk.Frame.__init__(self, master)
        self.createWidgets()

    def createWidgets(self):

        background_image = Tk.PhotoImage(file="C:\\Users\\chris\\Documents\\Uni\\Year Four\\Final Year Project\\bg.png")
        self.background_label = Tk.Label(root, image=background_image).place(x=0, y=0, relwidth=1, relheight=1)

        self.startButton = Tk.Button(root, text="Start Brewing!", command=startBrew).place(x=200,y=200)
        self.parametersButton = Tk.Button(root, text="Brewing Parameters", command=Application.parametersWindow).place(x=300,y=300)

        self.headerTemp = Tk.Label(root, textvariable=a).place(x=500,y=500)
        self.headerLev = Tk.Label(root, textvariable=text1).place(x=500,y=600)

        self.onUpdate()

    def parametersWindow():
        top = Toplevel()
        top.maxsize(400,400)
        top.title("Adjust Brew Parameters")
        about_message = "put some parameters in here"

        msg = Message(top, text=about_message).place(x=0,y=0)

        button = Button(top, text="Dismiss", command=top.destroy)

    def onUpdate(self):
        main()
        self.after(1000, self.onUpdate)

app = Application(master=root)
root.mainloop()

please scrutinise and correct where neccessary. but please keep in mind it is all new to me, may go over my head if the answer is too complex. Also any idea why text1 string var displays and yet i cannot get 'a' to display at all.

I've since tried this but it runs main() until cancelled and then allows the background and GUI to be displayed. please help im sure its a simple rule i'm not following:

    import time
from tkinter import *
import tkinter as tk   ## notice lowercase 't' in tkinter here

root = tk.Tk()
root.wm_geometry("1024x768")
root.title('Rpi Automated MicroBrewery')

a = StringVar()
a.set("1234")

text1 = StringVar()
text1.set("Header Full")

def startBrew():
    print('brew has started')

def a():
    print('a')
def b():
    print('b')
def c():
    print('c')

def main():
    a()
    b()
    c()
    root.after(5000, main())

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.createWidgets()

    def createWidgets(self):

        self.startButton = tk.Button(root, text="Start Brewing!", command=startBrew).place(x=200,y=200)
        self.parametersButton = tk.Button(root, text="Brewing Parameters", command=Application.parametersWindow).place(x=300,y=300)

        self.headerTemp = tk.Label(root, textvariable=a).place(x=500,y=500)
        self.headerLev = tk.Label(root, textvariable=text1).place(x=500,y=600)

        self.onUpdate()

    def parametersWindow():
        top = Toplevel()
        top.maxsize(400,400)
        top.title("Adjust Brew Parameters")
        about_message = "put some parameters in here"

        msg = Message(top, text=about_message).place(x=0,y=0)

        button = Button(top, text="Dismiss", command=top.destroy)

    def onUpdate(self):
        main()
        self.after(5000,onUpdate)

background_image = tk.PhotoImage(file="C:\\Users\\chris\\Documents\\Uni\\Year Four\\Final Year Project\\bg.png")
background_label = tk.Label(root, image=background_image).place(x=0, y=0, relwidth=1, relheight=1)
app = Application(master=root)
root.mainloop()

Thanks

chrisn89
  • 3
  • 3

1 Answers1

0

This is a common gotcha. You need to keep the image reference or it gets garbage collected. The easiest way to do that is to make it an instance variable by adding 'self.' to the variable name:

self.background_image = Tk.PhotoImage(file="C:\\Users\\chris\\Documents\\Uni\\Year Four\\Final Year Project\\bg.png")
self.background_label = Tk.Label(root, image=self.background_image)
self.background_label.place(x=0, y=0, relwidth=1, relheight=1)

Also, if you want to avoid future bugs, don't put the layout on the same line as the initialization. Split it into 2 lines like I did above.

To answer your other question, the "a" variable is not showing because you defined a function "a" later in the code that overwrote the StringVar "a". Let this be a lesson to you not to use single letter variable names. Always use descriptive names.

Novel
  • 13,406
  • 2
  • 25
  • 41
  • Thanks! I don't suppose you fancy helping me with the rest?? :') – chrisn89 Mar 27 '17 at 18:08
  • Did you see my edit? Was there something else? You may want to try reddit.com/r/learnpython if you have more questions, as it is more beginner oriented. – Novel Mar 27 '17 at 18:12
  • I did thanks. I'll have a look now. sometimes its hard to relate the examples to what i'm trying to achieve. I'm pretty much fumbling through at the moment due to the deadlines im working to. Thanks again for the help. – chrisn89 Mar 27 '17 at 18:59