How do avoid the global statement in python

I am completely new to python (know C, C++ and Delphi).
I have coded this little example (code last in message), trying to learn python.

It should (and does) open a window 500x500 with a counter in the upper left corner, current date and time in the lower left corner and the current window size in the lower right corner.

So far so good.

When I check my code with Pylint it gives me:

C0103:invalid-name: 14,0: Constant name “tic” doesn’t conform to UPPER_CASE naming style

C0103:invalid-name: 19,4: Constant name “tic” doesn’t conform to UPPER_CASE naming style

W0603:global-statement: 19,4: Using the global statement

How do I avoid these messages?

I mean “tic” is not a constant it is a variable.

I would really like to avoid the global statement, but then I get an error saying that “tic” does not exist.

I am sure it is because I do not know how to code in python properly.

Anyone that can help me a little bit?

Thank in advance.

# -*- coding: utf-8 -*-
"""
Created on Wed Aug 10 08:01:10 2022

@author: LAC
"""

import tkinter as tk
import time

CANVAS_WIDTH = 500
CANVAS_HEIGHT = 500

tic = 0


def func_1():
    """ func_1 """
    global tic
    tic += 1
    label_1["text"] = str(tic)
    canvas.after(100, func_1)


def func_2(event):
    """ func_2 """
    label_2["text"] = "Test " + str(event.width) + " " + str(event.height)


def func_3():
    """ func_3 """
    _t = time.localtime()
    _ts = time.strftime("%Y-%m-%d %H:%M:%S", _t)
    label_3["text"] = _ts
    canvas.after(500, func_3)


if __name__ == "__main__":
    master = tk.Tk()
    master.title("Points")

    canvas = tk.Canvas(master, width=CANVAS_WIDTH, height=CANVAS_HEIGHT)
    canvas.pack(expand=tk.YES, fill=tk.BOTH)

    label_1 = tk.Label(master, text="label_1")
    canvas.create_window(0, 0, window=label_1)
    label_1.place(relx=0, rely=0, anchor="nw")

    label_2 = tk.Label(master, text="label_2")
    canvas.create_window(0, 0, window=label_2)
    label_2.place(relx=1.00, rely=1.00, anchor="se")

    label_3 = tk.Label(master, text="label_3")
    canvas.create_window(0, 0, window=label_3)
    label_3.place(relx=0.00, rely=1.00, anchor="sw")

    func_1()

    func_3()

    canvas.bind("", func_2)

    master.mainloop()

I am also learning python.
Try moving the variable tic to one of your functions and declaring it like this

global tic = 0

(Make sure to remove the line tic = 0 outside the function though)

Another thing I learned answering this question: The interpreter thinks you are making conflicting definitions as when you put a variable definition outside a function you are essentially saying it is a constant. Then later you try to change its definition with the use of the keyword global. And so that is a conflict.

If you need a glabal variable that you can write to, define it inside a function but with the addition of the word global added to it.
(I know, weird right?)

Hope It works for you.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.