Python tkinter focus_set SubMenu后点击返回按钮

问题描述:

我是tkinter的新手,我有3个标题窗口。 如果我从窗口1到窗口2去,focus_set工作,并 如果我从窗口2到window3,focus_set工作, 如果我回来从window3到窗口2,其重点不在于进入文本,这是我想重点。Python tkinter focus_set SubMenu后点击返回按钮

它的工作从window2到window1。 我的简单代码如下:我知道它有点多的代码,但它会帮助找到确切的错误。

我尝试过发现错误,无法工作。

from Tkinter import * 

UI_MainForm = '' 

Black = 'Black' 
Green = 'Green' 
Red = 'Red' 
Gray = 'Gray' 
entrytext = '' 
entrytext1 = '' 
entrytext2 = '' 

top = '' 
Subtop = '' 

entry = '' 
entry1 = '' 
entry2 = '' 


def printf(Message, Color): 
global UI_SB 
global UI_TxtBar 
UI_TxtBar.insert(END, Message, Color) 
UI_TxtBar.see("end") 
print Message 
return; 

def UserInput(): 
    global UI_MainForm 
    UI_MainForm = Tk() 

    #Initialization of Main Window  

    UI_MainForm.resizable(1,0) 
    UI_MainForm.geometry("300x575+500+50") 
    UI_MainForm.title("Window1") 
    UI_MainForm.transient() 
    UI_MainForm.deiconify() 


    # LabelFrame to add all the Menu menu Display items 
    labelframe = LabelFrame(UI_MainForm,text="Please select the below options:",width=300, height=100,bd = 2) 
    labelframe.pack(fill="both") 
    labelframe.config(relief=RIDGE) 

    # LabelFrame to add the User input text box and buttons 
    labelframe1 = LabelFrame(UI_MainForm,text="User Input:",width=300, height=100,bd = 2) 
    labelframe1.pack(pady=8,fill="both") 
    labelframe1.config(relief=FLAT) 

#adding Text Box 
# textbox = Text(labelframe1, height=1, width=35,wrap=WORD) 
# textbox.pack(padx=4, pady=4) 

#Entry the text and display 
    global entrytext 
    global entry 
    entrytext = StringVar() 
    entry = Entry(labelframe1,textvariable=entrytext,width=35) 
    entry.pack(padx = 1, pady = 5) 
    entry.focus_set() # which will highlight the box without mouse click 
    # entry.delete(0, 'end') 
    entry.selection_range(0, END) 
    Input = entrytext.get() 
    # self.text.insert(END, Input + '\n') 
    # self.scroll.config(Input = self.text.yview) 



    #Main Menu Label Display items 
    MainMenuDisplay = [{"ID": 1,"Description": "Display Data1"}, 
        {"ID": 2,"Description": "Display Data2"}] 

     for menu in MainMenuDisplay: 
      temp_text = '{0}. {1}'.format(menu['ID'], menu['Description']) 
      Label(labelframe, text=temp_text).pack(anchor = W) 


    ButtonOK = Button(labelframe1, text = "OK", command =OnButtonOK, width =15) 
    ButtonOK.pack(side = LEFT, padx = 15, pady = 15) 


    ButtonEXIT = Button(labelframe1, text = "EXIT", width =15) 
    ButtonEXIT.pack(side = RIGHT, padx = 15) 

    UI_MainForm.mainloop() 

    return; 


def OnButtonOK(): 
# UI_MainForm.withdraw() 

    Input = int(entrytext.get()) 

    if (Input == 1): 
    SubMenu(); 

    elif (Input == 2): 
    SED_Menu_Display(); 


    else: 
     print "The Input is not valid 
    return; 
def SubMenu(): 
# UI_MainForm.withdraw() 
entry.delete(0,END) #Delete the Main Menu entry text after click 
UI_MainForm.iconify() 
global top 
top = Toplevel(UI_MainForm) 
top.geometry("300x250+500+50") 
top.title("Window2") 

    NumericMenuDisplay = [{"ID": 1,"Description": "Display Numeric PIDs SubMenu 1"}] 


    labelframe = LabelFrame(top,text="Please select the below options:",width=300, height=150,bd = 2) 
    labelframe.pack(fill="both") 
    labelframe.config(relief=RIDGE) 



    for menu in NumericMenuDisplay: 
    temp_text = '{0}. {1}'.format(menu['ID'], menu['Description']) 
    Label(labelframe, text=temp_text).pack(anchor = W) 

top.resizable(1,0) 
top.grab_set() 

frame = Frame(top,width=300, height=100,bd = 2) 
frame.pack(fill = 'both',padx=3, pady=3) 
frame.config(relief=RIDGE) 


    Label(frame, text = "q: Quit this Menu (Back to Main Menu)").pack(padx = 10, pady = 5) 
    global entrytext1 
    entrytext1 = StringVar() 
    entry1 = Entry(frame,textvariable=entrytext1, width = 35) 
    entry1.pack(padx = 5, pady = 5) 
    entry1.focus_set() # which will highlight the box without mouse click 

    topButtonShow = Button(frame, text = 'Show', command = OpenSubMenu,width = 15) 
    topButtonBack = Button(frame, text = 'Back', command = OnChildClose,width = 15) 
    topButtonShow.pack(side = LEFT,padx = 25,pady = 5) 
    topButtonBack.pack(side = RIGHT,padx = 5,pady = 15) 


    return; 
def OpenSubMenu(): 
    Input = entrytext1.get() 

    if (Input == '1'): 
    NumericPIDsSubMenu1(); 
    elif (Input == 'q'): 
    BackMenu(); 
    else: 
    print "The Input is not valid" 
    return; 

def NumericPIDsSubMenu1(): 
entry.delete(0,END) #Delete the Main Menu entry text after click 
top.iconify() 
global Subtop 
Subtop = Toplevel(top) 
Subtop.geometry("475x600+500+15") 
Subtop.title("Window3") 
Subtop.grab_set() 
leftFrame = Frame(Subtop,width=300, height=100,bd = 2) 
leftFrame.grid(row=0, column=0, padx=0.1, pady=0.1) 
leftFrame.config(relief=RIDGE) 


frame = Frame(Subtop,width=400, height=150,bd = 1) 
frame.grid(row=1, column=0,padx=5,pady=2,columnspan=2) #columnspan is to combine the grid(row & column) 


frame1 = Frame(Subtop,width=450, height=170,bd = 1) 
frame1.grid(row=2, column=0,padx=5,pady=2,columnspan=2) #columnspan is to combine the grid(row & column) 
frame1.config(relief=RIDGE) 

Label(frame, text = "q:Quit this Menu (Back to Main Menu)").pack(padx = 10, pady = 5) 
global entrytext2 
entrytext2 = StringVar() 
entry2 = Entry(frame,textvariable=entrytext2, width = 35) 
entry2.pack(padx = 5, pady = 5) 
entry2.focus_set() 



topButtonShow = Button(frame, text = 'Show', command = OpenSubMenu1,width = 10) 
topButtonBack = Button(frame, text = 'Back', command = SubMenuChildClose,width = 10) 
topButtonShow.pack(side = LEFT,padx = 10,pady = 5) 
topButtonBack.pack(side = RIGHT,padx = 10,pady = 5) 


    data_items = [{"ID": 1,"Description": "Display1"}, 
        {"ID": 2,"Description": "Display2"}] 

    for i,readdta in enumerate(data_items,start=1): 

     temp_text = '{0}. {1:04X} - {2}'.format(i,readdta['ID'], readdta['Description']) 

     Label(leftFrame, text=temp_text).pack(anchor = W,padx=5) 

    return; 

    def OpenSubMenu1(): 
    global Input 
    Input = int(entrytext2.get() 
    return; 

def SED_Menu_Display(): 
    return; 
def OnChildClose(): 
    BackMenu(); 
    return; 

    def BackMenu(): 
    UI_MainForm.deiconify() #Go back to the Main Window 
    top.destroy() # Destroy the Child Window 
    UI_MainForm.grab_set() 
    entry.focus_set() 

def BackSubMenu(): 
    top.deiconify() #Go back to the Main Window 
    Subtop.destroy() # Destroy the Child Window 
    top.grab_set() 
    entry1.focus_set() 
    #entry2.selection_range(0, END) 
    return; 

def SubMenuChildClose(): 
    BackSubMenu(); 
    return; 


def Main(): 

    UserInput() 


Main() 

错误是:我知道错误的类型,但我想具体的代码,在那里我需要改变

Attribute Error: 'str' object has no attribute focus_set 
+1

你必须在阻止它运行代码不平衡报价。 –

+0

您还需要格式化您的代码以匹配缩进。 – Khristos

不平衡报价和压痕问题放在一边(假设你解决这些),什么是生产该错误是globallocal变量问题。

您首先创建了一个全局变量entry1 = '',它是一个string。后来在你的代码,你SubMenu()功能的范围内创建一个本地变量entry1

def SubMenu(): 
    #... 

    entry1 = Entry(frame,textvariable=entrytext1, width = 35) 
    entry1.pack(padx = 5, pady = 5) 
    entry1.focus_set() # which will highlight the box without mouse click 

    #... 
    return 

这个地方entry1变量是一个Tkinter的部件。

当创建(称呼):

def BackSubMenu(): 
    #.... 
    entry1.focus_set() 
    return 

entry1是不是在BackSubMenu()所以它在全球范围内,其中entry1是一个字符串,因此误差寻找局部范围。

要changeg entry1弗朗字符串到Tkinter的小部件的一个实例,使用global关键字在SubMenu()定义为你在代码中的其他地方进行;

def SubMenu(): 
    global entry1 #modify the global entry1 from string to tkinter entry instance 
    #... 

    entry1 = Entry(frame,textvariable=entrytext1, width = 35) 
    entry1.pack(padx = 5, pady = 5) 
    entry1.focus_set() # which will highlight the box without mouse click 

    #... 
    return 

你可能要考虑使用类来构建你的代码。

工作代码

from Tkinter import * 

UI_MainForm = '' 

Black = 'Black' 
Green = 'Green' 
Red = 'Red' 
Gray = 'Gray' 
entrytext = '' 
entrytext1 = '' 
entrytext2 = '' 

top = '' 
Subtop = '' 

entry = '' 
entry1 = '' 
entry2 = '' 


def printf(Message, Color): 
    global UI_SB 
    global UI_TxtBar 
    UI_TxtBar.insert(END, Message, Color) 
    UI_TxtBar.see("end") 
    print Message 
    return; 

def UserInput(): 
    global UI_MainForm 
    UI_MainForm = Tk() 

    #Initialization of Main Window  

    UI_MainForm.resizable(1,0) 
    UI_MainForm.geometry("300x575+500+50") 
    UI_MainForm.title("Window1") 
    UI_MainForm.transient() 
    UI_MainForm.deiconify() 


     # LabelFrame to add all the Menu menu Display items 
    labelframe = LabelFrame(UI_MainForm,text="Please select the below options:",width=300, height=100,bd = 2) 
    labelframe.pack(fill="both") 
    labelframe.config(relief=RIDGE) 

     # LabelFrame to add the User input text box and buttons 
    labelframe1 = LabelFrame(UI_MainForm,text="User Input:",width=300, height=100,bd = 2) 
    labelframe1.pack(pady=8,fill="both") 
    labelframe1.config(relief=FLAT) 

#adding Text Box 
# textbox = Text(labelframe1, height=1, width=35,wrap=WORD) 
# textbox.pack(padx=4, pady=4) 

#Entry the text and display 
    global entrytext 
    global entry 
    entrytext = StringVar() 
    entry = Entry(labelframe1,textvariable=entrytext,width=35) 
    entry.pack(padx = 1, pady = 5) 
    entry.focus_set() # which will highlight the box without mouse click 
    # entry.delete(0, 'end') 
    entry.selection_range(0, END) 
    Input = entrytext.get() 
    # self.text.insert(END, Input + '\n') 
    # self.scroll.config(Input = self.text.yview) 



    #Main Menu Label Display items 
    MainMenuDisplay = [{"ID": 1,"Description": "Display Data1"}, 
            {"ID": 2,"Description": "Display Data2"}] 

    for menu in MainMenuDisplay: 
     temp_text = '{0}. {1}'.format(menu['ID'], menu['Description']) 
     Label(labelframe, text=temp_text).pack(anchor = W) 


    ButtonOK = Button(labelframe1, text = "OK", command =OnButtonOK, width =15) 
    ButtonOK.pack(side = LEFT, padx = 15, pady = 15) 


    ButtonEXIT = Button(labelframe1, text = "EXIT", width =15) 
    ButtonEXIT.pack(side = RIGHT, padx = 15) 

    UI_MainForm.mainloop() 

    return; 


def OnButtonOK(): 
# UI_MainForm.withdraw() 

    Input = int(entrytext.get()) 

    if (Input == 1): 
     SubMenu(); 

    elif (Input == 2): 
     SED_Menu_Display(); 

    else: 
      print "The Input is not valid" 
    return 

def SubMenu(): 
    global entry1 #modify the global entry1 from string to tkinter entry instance 
    # UI_MainForm.withdraw() 
    entry.delete(0,END) #Delete the Main Menu entry text after click 
    UI_MainForm.iconify() 
    global top 
    top = Toplevel(UI_MainForm) 
    top.geometry("300x250+500+50") 
    top.title("Window2") 

    NumericMenuDisplay = [{"ID": 1,"Description": "Display Numeric PIDs SubMenu 1"}] 


    labelframe = LabelFrame(top,text="Please select the below options:",width=300, height=150,bd = 2) 
    labelframe.pack(fill="both") 
    labelframe.config(relief=RIDGE) 


    for menu in NumericMenuDisplay: 
     temp_text = '{0}. {1}'.format(menu['ID'], menu['Description']) 
     Label(labelframe, text=temp_text).pack(anchor = W) 

    top.resizable(1,0) 
    top.grab_set() 

    frame = Frame(top,width=300, height=100,bd = 2) 
    frame.pack(fill = 'both',padx=3, pady=3) 
    frame.config(relief=RIDGE) 


    Label(frame, text = "q: Quit this Menu (Back to Main Menu)").pack(padx = 10, pady = 5) 
    global entrytext1 
    entrytext1 = StringVar() 
    entry1 = Entry(frame,textvariable=entrytext1, width = 35) 
    entry1.pack(padx = 5, pady = 5) 
    entry1.focus_set() # which will highlight the box without mouse click 

    topButtonShow = Button(frame, text = 'Show', command = OpenSubMenu,width = 15) 
    topButtonBack = Button(frame, text = 'Back', command = OnChildClose,width = 15) 
    topButtonShow.pack(side = LEFT,padx = 25,pady = 5) 
    topButtonBack.pack(side = RIGHT,padx = 5,pady = 15) 


    return 

def OpenSubMenu(): 
    Input = entrytext1.get() 

    if (Input == '1'): 
     NumericPIDsSubMenu1(); 
    elif (Input == 'q'): 
     BackMenu(); 
    else: 
     print "The Input is not valid" 
    return 

def NumericPIDsSubMenu1(): 
    entry.delete(0,END) #Delete the Main Menu entry text after click 
    top.iconify() 
    global Subtop 
    Subtop = Toplevel(top) 
    Subtop.geometry("475x600+500+15") 
    Subtop.title("Window3") 
    Subtop.grab_set() 
    leftFrame = Frame(Subtop,width=300, height=100,bd = 2) 
    leftFrame.grid(row=0, column=0, padx=0.1, pady=0.1) 
    leftFrame.config(relief=RIDGE) 


    frame = Frame(Subtop,width=400, height=150,bd = 1) 
    frame.grid(row=1, column=0,padx=5,pady=2,columnspan=2) #columnspan is to combine the grid(row & column) 


    frame1 = Frame(Subtop,width=450, height=170,bd = 1) 
    frame1.grid(row=2, column=0,padx=5,pady=2,columnspan=2) #columnspan is to combine the grid(row & column) 
    frame1.config(relief=RIDGE) 

    Label(frame, text = "q:Quit this Menu (Back to Main Menu)").pack(padx = 10, pady = 5) 
    global entrytext2 
    entrytext2 = StringVar() 
    entry2 = Entry(frame,textvariable=entrytext2, width = 35) 
    entry2.pack(padx = 5, pady = 5) 
    entry2.focus_set() 



    topButtonShow = Button(frame, text = 'Show', command = OpenSubMenu1,width = 10) 
    topButtonBack = Button(frame, text = 'Back', command = SubMenuChildClose,width = 10) 
    topButtonShow.pack(side = LEFT,padx = 10,pady = 5) 
    topButtonBack.pack(side = RIGHT,padx = 10,pady = 5) 


    data_items = [{"ID": 1,"Description": "Display1"}, {"ID": 2,"Description": "Display2"}] 

    for i,readdta in enumerate(data_items,start=1): 

     temp_text = '{0}. {1:04X} - {2}'.format(i,readdta['ID'], readdta['Description']) 

     Label(leftFrame, text=temp_text).pack(anchor = W,padx=5) 

    return 

def OpenSubMenu1(): 
    global Input 
    Input = int(entrytext2.get()) 
    return 

def SED_Menu_Display(): 
    return 

def OnChildClose(): 
    BackMenu() 
    return 

def BackMenu(): 
    UI_MainForm.deiconify() #Go back to the Main Window 
    top.destroy() # Destroy the Child Window 
    UI_MainForm.grab_set() 
    entry.focus_set() 


def BackSubMenu(): 
    top.deiconify() #Go back to the Main Window 
    Subtop.destroy() # Destroy the Child Window 
    top.grab_set() 
    entry1.focus_set() 
    #entry2.selection_range(0, END) 
    return 

def SubMenuChildClose(): 
    BackSubMenu(); 
    return 


def Main(): 
    UserInput() 


Main() 
+0

感谢您的时间来解释详细信息..现在完美工作... –

+0

我很高兴它帮助。 – Khristos