2017-04-04 154 views
0

我是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

回答

0

不平衡報價和壓痕問題放在一邊(假設你解決這些),什麼是生產該錯誤是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