Python-Tkinter-当从另一个档案库引用时,动态Checkbutton不会获取值

时间:2018-07-11 02:59:27

标签: python tkinter

我遇到了这个问题:我可以从动态变量(检查按钮)获取值,其中整个程序仅在一个存档中,并且只有一个功能:

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

但是,如果我将代码分成两个档案,每个档案在一个窗口中,并且其中一个具有两个功能,那么我将从复选框中得到零值:

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

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

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

这与多个文件(存档)无关。这是在程序中多次使用Collapsed的副作用。您需要使用Tk()仅创建第一个窗口,并使用Tk()生成任何其他窗口。试试:

Toplevel()