Importing values from other codes into tkinter - python

0

Hello, I'm doing a great program and wanted to split the screens into other codes, I can already import the screens, but I need to get some values from other screens, but I can not do that.

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 everything in one file? Thanks

    
asked by anonymous 08.12.2018 / 16:59

1 answer

0

Based on your sample 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 in a single file or separating into multiple files will depend on what is simpler for you to maintain, or even the size of the project.

The only issue that requires a little more care is when working with files / scripts that will be imported, as you should use if __name__ == '__main__': to "separate" execution local from the import file:

link

    
09.12.2018 / 14:52