___ ___ erkimt Python - Tkinter - Dynamic CheckButton not take the value of the variable when referenced from another file / screen ______ qstntxt ___

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

%pre%

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

File 1: TwoWindows2

%pre%

File 2: TwoWindows2

%pre%

Can anyone help me with this?

    
______ azszpr313528 ___

Variables from the other file can be accessed normally if you import the other file - but not with the %code% syntax, and use the filename as a prefix:

For example, you could put all the variables in a file name %code% and use a line like this

%pre%

And inside that file you can access the variables with %code% and so on.

Your code is pretty messy, but sometimes doing is the only way to learn - I recommend a third file, why else can you to fall into a problem of circular import dependency, the way it is 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 form prefix, as I showed above): using "global" inside classes and outside functions does not make sense. (It would even make a difference, but one has to know a lot Python to know exactly what it is doing, and even then, it would hardly be the right way to do anything.)

    
___

0

I'm having the following problem: I can get the value of a dynamic variable (checkbutton) when the whole program is in a single 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 0 values of the checkboxes:

File 1: TwoWindows2

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()

File 2: TwoWindows2

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 anyone help me with this?

    
asked by anonymous 11.07.2018 / 03:02

1 answer

0

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

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

import sys_vinho_variaveis as var

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

Your code is pretty messy, but sometimes doing is the only way to learn - I recommend a third file, why else can you to fall into a problem of circular import dependency, the way it is 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 form prefix, as I showed above): using "global" inside classes and outside functions does not make sense. (It would even make a difference, but one has to know a lot Python to know exactly what it is doing, and even then, it would hardly be the right way to do anything.)

    
11.07.2018 / 14:46