Python - Tkinter - Dynamic Checkbutton does not take the value of the variable when referenced from another file/screen

Asked

Viewed 414 times

0

I’m having the following problem: I can get the value of a dynamic variable (checkbutton) when the whole program is in one file with only one function:

import string
from tkinter import *
import tkinter as tk
qtdvinhos = 3
linhacheck = 1
var=dict()
mostra = Tk()
mostra.geometry("650x400")
mostra.title("Sydvinn - Vinhos cadastrados")
def Lecheck(event):
    for x in range(1,qtdvinhos):
        stringcheck = 'Check ' + str(x) + ': ' +str(var[x].get())
        print(stringcheck)
for x in range(1,qtdvinhos):
    var[x]=IntVar()
    check1 = Checkbutton(mostra, variable=var[x])
    check1.grid(row=linhacheck, column=0)
    linhacheck= linhacheck+1
button_excluir = Button(mostra, text="Pega Valor")
button_excluir.bind("<Button-1>", Lecheck)
button_excluir.grid(row=4, column=4)

mostra.mainloop()

However if I separate the code into two files, each one on a screen and two modules, I can only get the values 0 of the checkboxes:

File 1: Duasjanelaspt2

import string
from tkinter import *
import tkinter as tk


class ChamaClasse:
    global Lecheck
    global var
    var={}
    global qtdvinhos
    qtdvinhos = 3

    def Lecheck(event):
        for x in range(1,qtdvinhos):
            stringcheck = 'Check ' + str(x) + ': ' +str(var[x].get())
            print(stringcheck)

    def Abrejanela(event):       
        global linhacheck
        linhacheck = 1
        mostra = Tk() 
        mostra.geometry("650x400")
        mostra.title("Sydvinn - Vinhos cadastrados")

        for x in range(1,qtdvinhos):
            var[x]=IntVar()
            check1 = Checkbutton(mostra, variable=var[x])
            check1.grid(row=linhacheck, column=0)
            linhacheck= linhacheck+1

        button_excluir = Button(mostra, text="Pega Valor")
        button_excluir.bind("<Button-1>", Lecheck)
        button_excluir.grid(row=4, column=4)

        mostra.mainloop()

Archive 2: Duasjanelaspt2

import string
from tkinter import *
import tkinter as tk
from DuasJanelasPT1 import ChamaClasse

novo = Tk()
novo.geometry("650x400")
novo.title("Sydvinn - Vinhos cadastrados")
checks = dict()


button_excluir = Button(novo, text="Chama o Mechama")
checks = button_excluir.bind("<Button-1>", ChamaClasse.Abrejanela)
button_excluir.grid(row=4, column=4)

novo.mainloop()

Can someone help me with this?

1 answer

1

Variables from the other file can be accessed normally if you import the other file - but not with the syntax from outro_arquivo import x, and use file name as prefix:

For example, you could put all variables in a file by name sys_vinho_variaveis.py and use a line like this

import sys_vinho_variaveis as var

And then inside this file you can access the variables with var.qtdvinhos and so on.

Your code is pretty messed up, but sometimes doing it is the only way to learn - I recommend a third file, because otherwise you can fall into a circular import dependency problem, the way you’re working.

Another note: the keyword "global" only makes sense within functions - and says that those variables, within the function, will be visible in that module (file) - (and thus will be visible from the other files using the prefix form, as shown above): use "global" within classes and outside functions makes no sense. (It would make a difference, but one has to know much Python to know exactly what you’re doing, and even then, it would hardly be the right way to do anything).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.