2017-06-17 12 views
0

ユーザがオプションを選択しなかった場合、コードは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) 
関連する問題