消息未打印Tkinter

时间:2017-06-17 12:59:32

标签: python tkinter

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

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

1 个答案:

答案 0 :(得分:0)

如果您要使用纯字符串,则应使用标签text选项textvariable

Label(mGui, text=error_message)

如果您想使用textvariable,则需要StringVar, IntVar等。

此外,在同一行上打包会返回None,因此如果您想稍后再次使用它,则应在创建后进行打包。

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