2017-06-17 93 views
0

当用户没有选择任何选项时,代码进入else,在if_button_ispressed函数中,然后我想在屏幕上打印一条错误消息。我想我做的都是正确的,但消息没有显示在屏幕上。消息未打印Tkinter

import sys 
from Tkinter import * 
import Image, ImageTk 
import ShareScreen 
import Menu_WatchAnothersScreen 

def if_button_is_pressed(): 
    global user_selection, mGui 
    if(user_selection.get() == 1): # if the user want to share his screen 
     mGui.destroy() 
     ShareScreen.run() 
     #menu() 
    if(user_selection.get() == 2): # if the user want to watch another`s screen 
     mGui.destroy() 
     Menu_WatchAnothersScreen.run() 
     #menu() 
    else: # if the user didn`t chose any option <--------------- HERE IS MY PROBLEM 
     error_message = "Please select one of the options" #<--------------- HERE IS MY PROBLEM 
     error_label = Label(mGui, textvariable=error_message).pack(anchor=CENTER) # prints error message <--------------- HERE IS MY PROBLEM 


def close(): # close the window 
    exit() 

def menu(): 

    global user_selection,mGui 

    mGui = Tk() 
    user_selection = IntVar() 

    menubar = Menu(mGui) # menu 

    filemenu = Menu(menubar, tearoff=0) # menu works 
    filemenu.add_command(label="Close", command=close) 
    menubar.add_cascade(label="File", menu=filemenu) 

    mGui.geometry('450x300+500+300') 
    mGui.title('Nir`s ScreenShare') # top of window 

    canvas = Canvas(mGui, width=500, height=150) 
    canvas.pack(pady = 10) 
    pilImage = Image.open("logo5.png") 
    image = ImageTk.PhotoImage(pilImage)# puts ScreenirShare`s logo on the menu 
    imagesprite = canvas.create_image(0, 0, image=image, anchor="nw") 
    Radiobutton(mGui, text="Share My Screen    ", variable=user_selection, value=1).pack(anchor=CENTER) # 1 - FIRST OPTION - Share My Screen 
    Radiobutton(mGui, text="Watch Another`s Screen", variable=user_selection, value=2).pack(anchor=CENTER, pady = 7.5)# 2- SECOND OPTION - Watch Another`s Screen 



    start_button = Button(mGui, text='Start', command=if_button_is_pressed).pack() # Start Button 


    mGui.config(menu=menubar) # menu helper 

    mGui.mainloop() 


menu() 

回答

0

如果你要使用普通字符串,你应该使用的标签不textvariabletext选项。

Label(mGui, text=error_message) 

如果你想使用textvariable,你需要StringVar, IntVar

此外,包装上同一线路返回None,让你,如果你想在以后再次使用它创建后,应该包。

error_label = Label(mGui, textvariable=error_message) 
error_label.pack(anchor=CENTER)