为什么默认文本输入Python Tk何时不起作用?

时间:2014-09-26 06:46:08

标签: python tkinter tkinter-entry

我想知道为什么默认文本不会出现在输入字段中。在同一个屏幕上一切正常。问题是当我从另一个文件调用该函数时。我有这个调用函数的菜单,一切都很好,但不是默认文本主菜单文件夹中的导入文件这是我的代码:

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

这是主文件的代码。 要测试代码,您需要创建名为manteniment的文件夹 并在开头和结尾放入一个带有下划线的空文件__init__.py

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

1 个答案:

答案 0 :(得分:0)

问题是您正在创建多个Tk的实例。 tkinter程序应该只有Tk的单个实例。如果要创建多个窗口,则其他窗口必须是Toplevel的实例。

在mnuArticles中,创建Toplevel而不是Tk的实例:

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

您还需要删除mnuArticles函数中对root.mainloop()的调用,因为您的主程序已经运行了主循环。