消息未打印 Tkinter



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

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()
如果要

使用纯字符串,则应使用标签而不是textvariable text选项。

Label(mGui, text=error_message)

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

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

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

最新更新