Search code examples
pythontkintertkinter-entry

Why default text entry Python Tk not work when is a function?


I would like to know why the default text does not appear on the entry field. On the same screen everything works fine. The problem is when I call the function from another file. I have this menu which calls the function and everything appears fine, but not the default text The main menu file with the import files within foldersThis is my code:

__author__ = 'jordiponsisala'
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import*



def mnuArticles():

    def provaD():
        print('Imprimiendo algo')
        print(entDescripcio.get())

    root = Tk()
    root.resizable(0,0)

    notebook = ttk.Notebook(root)
    notebook.pack(fill=BOTH, expand=True,)
    notebook.pressed_index = None
    notebook.master.title("Manteniment d'Articles")
    notebook.master.geometry('900x650+0+100')


    container1 = Frame(notebook,bg='grey')
    container2 = Frame(notebook)


    notebook.add(container1, text='Article')

    botoImprimir = tk.Button
    botoImprimir(container1,text='Provando',highlightbackground='grey'
                 ,command=provaD).place(x=650,y=450)

    tk.Label(container1,text='Codig',bg='grey').place(x=45,y=5)
    tk.Label(container1,text='Descripció',bg='grey').place(x=200,y=5)


    entArticle = StringVar()
    entDescripcio = StringVar()
    entDescripcio.set('the default text that does not appear')


    txtArticle = Entry(container1,textvariable=entArticle
                       ,width=10,highlightthickness='0').place(x=100,y=0)

    txtDescripcio = Entry(container1,textvariable= entDescripcio
                          ,width=50,highlightthickness='0').place(x=280,y=0)


    notebook.add(container2, text='Preu')


    root.mainloop()

This is the code of the main file. To test the code you need to create a folder with the name manteniment and put inside an empty file __init__.py with underscores at the beginning and end

from tkinter import *
from manteniment.articles import *

ventana = Tk()
ventana.geometry ('500x500+0+0')
ventana.title ('Benvinguts')
lblVentana = Label(text='Grub article').pack()

barraMenu = Menu (ventana)

mnuArchivo = Menu (barraMenu)
mnuTpv = Menu (barraMenu)
mnuLListats = (barraMenu)

mnuArchivo.add_command (label='Articles',command=mnuArticles) #I call the function here

barraMenu.add_cascade(label = 'Mantenimiento',menu =mnuArchivo)
barraMenu.add_cascade(label = 'TPV', menu = mnuTpv)

ventana.config(menu = barraMenu)

ventana.mainloop()

Solution

  • The problem is that you're creating more than one instance of Tk. A tkinter program should only ever have a single instance of Tk. If you want to create multiple windows, additional windows need to be instances of Toplevel.

    In mnuArticles, create an instance of Toplevel rather than Tk:

    def mnuArticles():
        ...
        root = Toplevel()
        ...
    

    You also need to remove the call to root.mainloop() in the mnuArticles function since your main program already has a mainloop running.