Python - Tkinter - Dynamic Checkbutton don't get the value when referenced from another archive

I'm having this issue: I can get the value from a dynamic variable (checkbutton) where the whole program is in just one archive with just 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()

But if I separate the code in two archives, each one in one window and one of them with two functions, I will just get the value zero from the checkboxes:

Archive 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: DuasJanelasPT1

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?

1 answer

  • answered 2018-07-11 04:04 Novel

    This has nothing to do with multiple files (archives). This is a side effect of using Tk() more than once in your program. You need to use Tk() to make the first window only, and Toplevel() to make any additional windows. Try:

    mostra = Toplevel()