简体   繁体   中英

I want to add error page when form is not filled in python tkinter

First, let me show you the project

from tkinter import *
import os

window = Tk()
window.title("Social Searcher")
window.wm_attributes('-toolwindow', 'True')

Tk_Width = 350
Tk_Height = 150
 
positionRight = int( window.winfo_screenwidth()/2 - Tk_Width/2 )
positionDown = int( window.winfo_screenheight()/2 - Tk_Height/2 )
window.geometry("{}x{}+{}+{}".format(330,100,positionRight, positionDown))



stxt = Label(window, text = "username").place(x = 30, y = 10)  
s = Entry(window, width=30)
s.insert(0,"")
s.grid(row=1, column=0, padx=100, pady=10)

def Message():
    os.system(f"If Not Exist \"History.txt\" (echo Social Searcher history)>\"History.txt\"")
    os.system(f"If Exist \"History.txt\" (echo {s.get()})>>\"History.txt\"")
    os.system(f"start https://www.facebook.com/{s.get()}")
    os.system(f"start https://www.instagram.com/{s.get()}")
    os.system(f"start https://www.tiktok.com/@{s.get()}")
    os.system(f"start https://twitter.com/{s.get()}")
    os.system(f"start https://story.snapchat.com/@{s.get()}")

btnSendMessage = Button(window, text="Search", width=20, command=Message)
btnSendMessage.grid(row=5, column=0, padx=10, pady=10)

window.mainloop()

Currently, if I don't enter anything in the (username) field, the code will work.

If I don't put anything I want to show me an error page.

I tried to put this code here

def Message():
    os.system("if ( {s.get()} -eq ""){exit}")
    os.system(f"If Not Exist \"History.txt\" (echo Social Searcher history)>\"History.txt\"")
    os.system(f"If Exist \"History.txt\" (echo {s.get()})>>\"History.txt\"")
    os.system(f"start https://www.facebook.com/{s.get()}")
    os.system(f"start https://www.instagram.com/{s.get()}")
    os.system(f"start https://www.tiktok.com/@{s.get()}")
    os.system(f"start https://twitter.com/{s.get()}")
    os.system(f"start https://story.snapchat.com/@{s.get()}")

But it didn't work

Answer from @Matiiss

from tkinter import *
import os

window = Tk()
window.title("Social Searcher")
window.wm_attributes('-toolwindow', 'True')

Tk_Width = 350
Tk_Height = 150
 
positionRight = int( window.winfo_screenwidth()/2 - Tk_Width/2 )
positionDown = int( window.winfo_screenheight()/2 - Tk_Height/2 )
window.geometry("{}x{}+{}+{}".format(330,100,positionRight, positionDown))



stxt = Label(window, text = "username").place(x = 30, y = 10)  
s = Entry(window, width=30)
s.insert(0,"")
s.grid(row=1, column=0, padx=100, pady=10)

def Message():
    if not s.get(): return
    os.system(f"If Not Exist \"History.txt\" (echo Social Searcher history)>\"History.txt\"")
    os.system(f"If Exist \"History.txt\" (echo {s.get()})>>\"History.txt\"")
    os.system(f"start https://www.facebook.com/{s.get()}")
    os.system(f"start https://www.instagram.com/{s.get()}")
    os.system(f"start https://www.tiktok.com/@{s.get()}")
    os.system(f"start https://twitter.com/{s.get()}")
    os.system(f"start https://story.snapchat.com/@{s.get()}")

btnSendMessage = Button(window, text="Search", width=20, command=Message)
btnSendMessage.grid(row=5, column=0, padx=10, pady=10)

window.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM