【问题标题】:I want to add error page when form is not filled in python tkinter当表单未填写 python tkinter 时,我想添加错误页面
【发布时间】:2021-11-10 11:21:49
【问题描述】:

首先,让我向您展示项目

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

目前,如果我没有在(用户名)字段中输入任何内容,代码将起作用。

如果我什么都不放,我想显示一个错误页面。

我试着把这段代码放在这里

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

但是没用

【问题讨论】:

  • os.system 多年来已被弃用。使用subprocess 模块可以完全控制执行的应用程序、输入、输出和错误流。但是,所有的 os.system 调用都可以用本机 Python 代码替换。该任务根本不需要使用 Windows 命令处理器 cmd.exe。 Python 是比 CMD 更强大的脚本解释器。所以不要使用Python在后台运行cmd.exe执行的CMD命令行。
  • 您可以在Message 函数中放置一个简单的if not s.get(): return,如果Entry 中没有输入任何内容,则不允许继续执行该函数,然后您可以在之前添加更多代码return 将简单地放置一些标签,说明应该输入一些东西或类似的东西
  • Message()内的所有任务都可以通过文件IO创建History.txtwebbrowser模块来打开网页。

标签: python tkinter os.system


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-17
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 2019-03-21
    相关资源
    最近更新 更多