Importing values from other codes in Tkinter - python

Asked

Viewed 354 times

0

Hello, I’m doing a large program and wanted to divide the screens into other codes, I can import the screens, but I need to get some values from other screens, but I can’t do it.

First screen:

from tkinter import *
import teste_interface2
def teste():
    teste_interface2.tela_teste()

def tela():
    janela = Tk()
    janela.geometry("300x300+300+300")
    lb1 = Label(janela, text = "Insira letras,testando a exportação:").pack()
    global ed1_interface1
    ed1_interface1 = Entry(janela).pack()
    bt = Button(janela, text = "exportar", command = teste).pack()

tela()

Second screen

from tkinter import*
import teste_interface1

def importacao():
    teste_import = ed1_interface1.get()
    lb = Label(janela1, text = teste_import).pack()

def tela_teste():
    global janela1
    janela1 =  Tk()
    janela1.geometry("300x300+300+300")
    bt1 = Button(janela1, text = "importar", command = importacao).pack()

Is it possible to do this export/import or is it easier to leave it all in one file? Thank you

  • What exactly do you want to import, @Antoniogally? A function, a variable, etc

  • I want to export the value of a variable

1 answer

0

Based on your example code one of the possibilities would be:

First screen:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""teste_interface1.py"""
import tkinter as tk

import teste_interface2


def abrir_tela_2(instance, widget):
    valor = widget.get()
    instance.destroy()
    teste_interface2.tela2(valor)


def tela1(entry=None):
    janela = tk.Tk()
    janela.geometry("300x300+300+300")
    janela.title('Teste interface 1')

    valor_entry_interface2 = "Valor que estava no Entry da tela 1: %s" % entry
    if entry is None:
        valor_entry_interface2 = "Bem vindo"

    label = tk.Label(janela, text=valor_entry_interface2)
    label.pack()

    label_interface1 = tk.Label(janela, text="Insira letras,testando a exportação:")
    label_interface1.pack()

    entry_interface1 = tk.Entry(janela)
    entry_interface1.pack()

    button_interface1 = tk.Button(janela, text="exportar",
                                  command=lambda: abrir_tela_2(instance=janela, widget=entry_interface1))
    button_interface1.pack()

    janela.mainloop()


if __name__ == '__main__':
    tela1()

Second screen:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""teste_interface2.py"""
import tkinter as tk

import teste_interface1


def abrir_tela_1(instance, widget):
    valor = widget.get()
    instance.destroy()
    teste_interface1.tela1(valor)


def tela2(entry=None):
    janela = tk.Tk()
    janela.geometry("300x300+300+300")
    janela.title('Teste interface 2')

    valor_entry_interface1 = "Valor que estava no Entry da tela 1: %s" % entry
    if entry is None:
        valor_entry_interface1 = "Bem vindo"

    label = tk.Label(janela, text=valor_entry_interface1)
    label.pack()

    label_interface2 = tk.Label(janela, text="Insira letras,testando a exportação:")
    label_interface2.pack()

    entry_interface2 = tk.Entry(janela)
    entry_interface2.pack()

    button_interface2 = tk.Button(janela, text="exportar",
                                  command=lambda: abrir_tela_1(instance=janela, widget=entry_interface2))
    button_interface2.pack()

    janela.mainloop()


if __name__ == '__main__':
    tela2()

Leaving it in a single file or separating it into several will depend on what is simpler for you to maintain or even size of the project.

The only question that demands a little more care is when working with files/scripts that will be imported, because you should use if __name__ == '__main__': for "separate" the execution local of the archive of importing:

/a/92589/93029

Browser other questions tagged

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