2014-09-26 37 views
0

我想知道为什么默认文本不会出现在输入字段。在同一屏幕上一切正常。问题是当我从另一个文件调用函数时。我有这个菜单,调用函数,一切显示正常,但不是默认的文本与内foldersThis导入文件的主菜单文件是我的代码:为什么默认文本条目Python Tk在函数中不起作用?

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

回答

0

该问题的代码是,你要创建一个以上Tk的实例。一个tkinter程序应该只有一个Tk的实例。如果你想创建多个窗口,额外的窗口需要是Toplevel的实例。

在mnuArticles,创造Toplevel而不是Tk一个实例:

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

您还需要去掉调用root.mainloop()在mnuArticles功能,因为您的主要程序已经有一个主循环运行。

+0

这是最好的解决方案,我学到了很好的一课。 我喜欢python和tkinter哈哈。 – 2014-09-26 20:54:49

相关问题