Woody_99
Posts: 7
Joined: Sat Nov 11, 2017 4:18 pm

Problems with the code (while-loop)

Tue Nov 14, 2017 2:39 pm

Hello,

Code: Select all

import tkinter as tk

global Seite
Seite=0
x=4 #maximale Seiten
i=1

def Seiteplus():
    Seite=Seite+1
    if Seite > x:
        Seite=0

while True:

    if i==1:
        root=tk.Tk()
        root.attributes('-fullscreen', True)
        root.wm_title("Glashaus") # Fenster Titel
        root.config(background = "#FFFFFF") # Hintergrundfarbe des Fensters
        i=0
    
    nextbtn = tk.Button(root, text='►', fg='black',height=1,font=('Arial', 40), command=Seiteplus) #width=10
    nextbtn.pack(anchor='ne')


    lastbtn = tk.Button(root, text='◄', fg='black',height=1, font=('Arial', 40), command=Seiteplus)
    lastbtn.pack(anchor='ne')

    exitbtn = tk.Button(root, text= Seite, fg='black', height=2, command=root.destroy)
    exitbtn.pack(side='bottom', anchor='ne')

    Emailanweisung = tk.Label(root, text="Emailadresse für Nachrichten eingeben:", font=('Arial', 30))
    Emailanweisung.pack()

    textfeld = tk.Text(root, width=35, height=1, bg="#FFFFFF",font=('Arial', 30))
    textfeld.pack(side='top', anchor='center')

    print("LOL")

    Seite = tk.Label(root, text=Seite, font=('Arial', 30))
    Seite.pack()
    root.mainloop()
When i Press the Button (nextbtn), i get the following message:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\XXX\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "C:\Users\XXX\AppData\Local\Programs\Python\Python36-32\Programme\World.py", line 10, in Seiteplus
Seite=Seite+1
UnboundLocalError: local variable 'Seite' referenced before assignment
Also, the while-loop is passed only once. Can anyone identify the problem or problems?
Thanks

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Problems with the code (while-loop)

Tue Nov 14, 2017 3:08 pm

Put

Code: Select all

global seite
before

Code: Select all

seite = seite + 1
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

User avatar
B.Goode
Posts: 8987
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Problems with the code (while-loop)

Tue Nov 14, 2017 3:10 pm

Code: Select all

File "C:\Users\XXX\AppData\Local\Programs\Python\Python36-32\Programme\World.py", line 10, in Seiteplus
Seite=Seite+1
UnboundLocalError: local variable 'Seite' referenced before assignment
The Python interpreter is always correct!

Within the function Seiteplus() that variable is not initialised before you try to increment it.


I am not at all expert in the use of tkinter, but I have a feeling that you should not be trying to start the GUI mainloop() within the scope of a while statement.


(This must be a knowledgeable and friendly forum if you feel able to pose questions arising from your use of a Microsoft Windows system here... )

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: Problems with the code (while-loop)

Tue Nov 14, 2017 3:57 pm

Ha. Hadn't noticed the file path!
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

hippy
Posts: 6223
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Problems with the code (while-loop)

Tue Nov 14, 2017 5:06 pm

B.Goode wrote:
Tue Nov 14, 2017 3:10 pm
(This must be a knowledgeable and friendly forum if you feel able to pose questions arising from your use of a Microsoft Windows system here... )
Most of my Pi Python code is developed, tested and debugged on a Windows PC then copied over to my Pi's.

I suppose that's my familiarity with Windows over Linux, partly my background in embedded systems where development and target are traditionally separate, and that I mostly treat my Pi's as application engines rather than development machines.

The main reason though is it's easier for me to develop on a desktop Windows PC than on my knees in front of my Pi's connected to the TV.

Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

Re: Problems with the code (while-loop)

Tue Nov 14, 2017 6:39 pm

Code: Select all

import tkinter as tk

Seite = 0
x = 4  # maximale Seiten
i = 1


def Seiteplus():
    global Seite
    Seite += 1
    if Seite > x:
        Seite = 0


while True:

    if i == 1:
        root = tk.Tk()
        root.attributes('-fullscreen', True)
        root.wm_title("Glashaus")  # Fenster Titel
        root.config(background="#FFFFFF")  # Hintergrundfarbe des Fensters
        i = 0

    nextbtn = tk.Button(root, text='►', fg='black', height=1, font=('Arial', 40), command=Seiteplus)  # width=10
    nextbtn.pack(anchor='ne')

    lastbtn = tk.Button(root, text='◄', fg='black', height=1, font=('Arial', 40), command=Seiteplus)
    lastbtn.pack(anchor='ne')

    exitbtn = tk.Button(root, text=Seite, fg='black', height=2, command=root.destroy)
    exitbtn.pack(side='bottom', anchor='ne')

    Emailanweisung = tk.Label(root, text="Emailadresse für Nachrichten eingeben:", font=('Arial', 30))
    Emailanweisung.pack()

    textfeld = tk.Text(root, width=35, height=1, bg="#FFFFFF", font=('Arial', 30))
    textfeld.pack(side='top', anchor='center')

    print("LOL")

    SeiteLabel = tk.Label(root, text=Seite, font=('Arial', 30))
    SeiteLabel.pack()
    root.mainloop()
if your using a global variable you need to call it as so within the def,
also the bottom label had the same name as your variable, redefining it as a label and failing thus forth as a variable.
this code runs without an error but doesnt reflect what your wanting, for this youll need to call a config of your label and root.update_idletasks to refresh your tkinter window

Code: Select all

def Seiteplus():
    global Seite
    Seite += 1
    if Seite > x:
        Seite = 0
    SeiteLabel.config(text=Seite)
    root.update_idletasks()

scotty101
Posts: 3750
Joined: Fri Jun 08, 2012 6:03 pm

Re: Problems with the code (while-loop)

Wed Nov 15, 2017 11:39 am

B.Goode wrote:
Tue Nov 14, 2017 3:10 pm
I am not at all expert in the use of tkinter, but I have a feeling that you should not be trying to start the GUI mainloop() within the scope of a while statement.
Indeed. The OP should not be trying to put tkinter code inside a while loop at all.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Python”