我的任务是创建一个关于移动device.the GUI的用户问题的GUI,我已经创建了一系列的问题来尝试使用solution.However,我觉得我在GUI方面的经验让我重复了很多次。因此,我能做些什么来提高效率吗?我希望使这段代码尽可能高效,同时保留它的功能。
from tkinter import *
import sys
screen = Tk()
def sftwr_or_hrdwr():
label6=Label(screen, text = "is your phone a software or hardware problem?",font=("Helvetica",15))
label6.pack()
software=Button(screen,fg="black",bg="white",text="SOFTWARE", command = question1_s)
software.pack()
hardware=Button(screen,fg="black",bg="white",text="HARDWARE",command = question1_h)
hardware.pack()
IDK=Button(screen, fg = "black", bg= "white", text ="I DONT KNOW THE DIFFERENCE",command=IDK_)
IDK.pack()
def IDK_():
label27=Label(screen,text="hardware are the physical components of the phone(camera, internal parts ect) \n the software is the oporating system. eg, apple phones yous OSX, samsung phones use android. \n common faults for this are, phone freezing/shutting down)",font=("Helvetica",15))
label27.pack()
label28=Label(screen,text="now go back and click either software or hardwre",font=("Helvetica",15))
label28.pack()
#software questions
def question1_s():
label5 = Label( screen, text = "Has your phone been randomly shuttting down ? ",font=("Helvetica",15))
label5.pack()
Yes2 = Button(screen, fg = "black", bg= "white", text = "YES",command =solution1_s)
Yes2.pack()
No2 = Button(screen, fg = "black", bg = "white", text = "NO", command= question2_s)
No2.pack()
def question2_s():
label21=Label(screen, text="has your phone been getting pop ups blocking your screen constantly, and applications being randomly deleted (even with storage available)?",font=("Helvetica",15))
label21.pack()
Yes_all=Button(screen, fg = "black", bg= "white", text ="YES, all of these are happening", command= solution2_s)
Yes_all.pack()
Yes_app=Button(screen, fg = "black", bg= "white", text ="YES, but only applications being deleted",command=solution_app)
Yes_app.pack()
Yes_pop=Button(screen, fg = "black", bg= "white", text ="YES, but only the pop up ads",command=solution_pop)
Yes_pop.pack()
No8=Button(screen, fg = "black", bg= "white", text ="NO none of these",command= question3_s)
No8.pack()
def question3_s():
label25=Label(screen,text="has your phone been freezing randomly?",font=("Helvetica",15))
label25.pack()
Yes9=Button(screen, fg = "black", bg= "white", text ="YES",command=solution3_s)
Yes9.pack()
No9=Button(screen, fg = "black", bg= "white", text ="NO",command=no_solution)
No9.pack()
#hardware questions
def question1_h():
label3 = Label(screen, text = " Has your phone got a crack in it ? ",font=("Helvetica",15))
label3.pack()
Yes1 = Button(screen, fg = "black", bg= "white", text = "YES",command=hardware_crack2)
Yes1.pack()
No1 = Button(screen, fg = "black", bg = "white", text = "NO", command =question2_h )
No1.pack()
def hardware_crack1():
label9=Label(screen,text="is the crack showing any internal parts of thr system?",font=("Helvetica",15))
label9.pack()
Yes3=Button(screen, fg = "black", bg= "white", text ="YES", command= solution_crack1)
No3=Button(screen, fg = "black", bg= "white", text ="NO",command= hardware_crack2)
def hardware_crack2():
label8=Label(screen,text= "where is the crack on your phone?",font=("Helvetica",15))
label8.pack()
crack=Button(screen, fg = "black", bg= "white", text ="on the main screen", command=solution_crack2)
crack.pack()
back=Button(screen, fg = "black", bg= "white", text ="on the back of the phone",command=solution_crack3)
back.pack()
on_c=Button(screen, fg = "black", bg= "white", text ="on a component (eg, crack on the camera)", command=solution_crack4)
on_c.pack()
sides=Button(screen, fg = "black", bg= "white", text ="on the side of the phone/around but not on the screen", command=solution_crack3)
sides.pack()
def question2_h():
label12=Label(screen,text="has your phone come in contact with liquid?",font=("Helvetica",15))
label12.pack()
Yes4=Button(screen, fg = "black", bg= "white", text ="YES", command=solution2_h)
Yes4.pack()
No4=Button(screen, fg = "black", bg= "white", text ="NO",command=question3_h)
No4.pack()
def question3_h():
label15=Label(screen,text="is your phone not charging correctly, or loosing battery very fast or jumping from battery percentages?",font=("Helvetica",15))
label15.pack()
Yes5=Button(screen, fg = "black", bg= "white", text ="YES",command=solution3_h)
Yes5.pack()
No5=Button(screen, fg = "black", bg= "white", text ="NO", command=question4_h)
No5.pack()
def question4_h():
label17=Label(screen,text="is your phone overheating or unable to open many applications at once?",font=("Helvetica",15))
label17.pack()
Yes_o=Button(screen, fg = "black", bg= "white", text ="YES, it has been overheating but it can open applcations fine",command=solution_heat)
Yes_o.pack()
Yes_a=Button(screen, fg = "black", bg= "white", text ="YES, there are no overheating problems, but i am struggling to open many applications at once",command=solution_RAM)
Yes_a.pack()
Yes7=Button(screen, fg = "black", bg= "white", text ="YES, both of these are happening to me", command =solution4_h)
Yes7.pack()
No7=Button(screen, fg = "black", bg= "white", text ="NO", command=no_solution)
No7.pack()
#software solutions
def solution1_s():
label20=Label(screen,text="if this is happeing to you, there is a bug in the software. call up your supplier and they will try and troubleshoot the phone. make sure you have all the latest updates installed",font=("Helvetica",15))
label20.pack()
sys.exit()
def solution2_s():
label22=Label(screen,text="these are all signs of a virus. the deleting of applications is virus munching on your data, and pop ups on your scren is also a virus symptom. immiditely use your antivirus to look for the problem or take it to a repair shop where they can delete the virus",font=("Helvetica",15))
label22.pack()
sys.exit()
def solution_app():
label23=Label(screen,text="check if you have enogh storage on your device, if you dont and that was the problem, then see if you can get a storage upgrade. However, if it isnt there is a good chance you have a virus on your phone. scan your phone with an antivirus, or let your local repair shop do it",font=("Helvetica",15))
label23.pack()
sys.exit()
def solution_pop():
label24=Label(screen,text="f the pop ups are on a web browser, this is normal. try getting an ad blocker if it is bothering you, but do not click on them. however, if it is happening on the main screen, you have contracted a virus. use your antivirus orget it fixed at a repair shop",font=("Helvetica",15))
label24.pack()
sys.exit()
def solution3_s():
label26=Label(screen,text="this is another sign of a software fault. the only one who can fix this is your supplier. contact them as soon as possible",font=("Helvetica",15))
label26.pack()
sys.exit()
#hardware solutions
def solution_crack1():
label10=Label(screen,text="take it to a repair shop as soon as possible as if interior parts are exposed then it can damage your phone",font=("Helvetica",15))
label10.pack()
sys.exit()
def solution_crack2():
label11=Label(screen,text=" get your screen repaired at a local repair shop, as parts of the screen glass may fall off",font=("Helvetica",15))
label11.pack()
sys.exit()
def solution_crack3():
label12=Label(screen,text="Try and get a protective cover that can protect the back/sides of your phone. you can also get a screen protector if you are worred about moe dents. or get it repaired at a repair shop",font=("Helvetica",15))
label12.pack()
sys.exit()
def solution_crack4():
label13=Label(screen,text="if your hardware is getting distrupted due to cracks and dents, you should go and get it repaired at a shop, as it is limiting what your phone can do",font=("Helvetica",15))
label13.pack()
sys.exit()
def solution2_h():
label2 = Label(screen, text = " Please take off your cover and try to dry the inner components.",font=("Helvetica",15))
label2.pack()
sys.exit()
def solution3_h():
label16=Label(screen,text= "you should get your battery changed. try and find a battery that is compatible with your phone by asking your supplier, and get a technitian to replace it if need be.",font=("Helvetica",15))
label16.pack()
sys.exit
def solution4_h():
label18=Label(screen,text="this means there is a problem with the internal parts. this could be that the parts are becoming old. The struggle to open applications is the ram. this could be that it is getting old and is loosing performance or it is damaged. The overheating is the other components in your phone are not functioning properly. you should contant your supplier and get it repaired at a repair shop.",font=("Helvetica",15))
label18.pack()
sys.exit()
def solution_RAM():
label19=Label(screen,text="the fact you cant open multiple applications at once means your RAM is not functioning properly. get it upgraded or replaced at a repair shop",font=("Helvetica",15))
label19.pack()
sys.exit()
def solution_heat():
label20=Label(screen,text="if your phone is overheating that means the internal parts are not performing how they should be. ask your local repair shop to see what is wrong with the internal parts",font=("Helvetica",15))
label20.pack()
#no solution
def show_entry_fields():
print("Email: %s\nProblem with phone: %s" % (e1.get(), e2.get()))
def no_solution():
label21=Label(screen,text="sorry, we cannot find a solution to your problem. try to see if your problem is in hardware/software as you may have picked the wrong one. do ths by scrolling up and clicking the other option. your data will be sent to a technitian and we will get back to you as soon as possible. in the box submit our email and problem, then press Enter and close down troubleshooter",font=("Helvetica",15))
label21.pack()
master = Tk()
Label(master, text="Email").grid(row=0)
Label(master, text="Problem with phone").grid(row=1)
e1 = Entry(master)
e2 = Entry(master)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
Button(master, text='Quit', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Enter', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)def start_program():
label = Label(screen, text = "Hello and welcome to the troubleshooting program ",font=("Helvetica",15))
label.pack()
label1 = Label(screen, text = "")
label.pack()
sftwr_or_hrdwr()
screen.title("Troubleshooter")
screen.configure(background="#a1dbcd")
screen.geometry("1920x1080")
start = Button(screen, fg = "black", bg = "white", text = "Lets get started with troubleshooter ",font= ("Helvetica",16), command = start_program)
start.pack()当然,由于这是一个项目,我实际上并不期望电子邮件发送给用户。我对python相当陌生,但是我需要尽可能地提高效率,任何帮助都是非常感谢的。谢谢!
发布于 2017-01-11 14:28:56
在创建程序时,首先要考虑的是数据结构。在您的例子中,您没有任何数据结构;相反,您只有一堆代码。
例如,很多代码遵循这样的模式:
def solution2_h():
label2 = Label(screen, text = " Please take off your cover and try to dry the inner components.",font=("Helvetica",15))
label2.pack()
sys.exit()因为所有这些解决方案之间唯一的区别是文本,所以可以从函数中删除文本并使文本成为参数。在这种情况下,我建议编写解决方案文本的字典:
solutions = {
"2_h": "Please take off your cover and try to dry the inner components"
"2_s": "these are all signs of a virus. ..."
...
}然后,您可以创建一个显示解决方案的函数:
def show_solution(name):
label = Label(screen, text=solutions[name])
label2.pack()
sys.exit()您的代码似乎反映了您键入代码的方式。“我需要一个标签”,“我需要包装标签”,“我需要另一个标签”,“我需要包装标签”。相反,关于代码的事情更符合逻辑。
GUI由几个逻辑部分组成。其中一部分是创建GUI所需的实际小部件。另一部分是如何在屏幕上组织这些小部件并呈现给用户。
我的建议是将所有布局代码集合在一起,由小部件层次结构组织。例如:
label6=Label(screen, ...)
software=Button(screen,...)
hardware=Button(screen,...)
IDK=Button(screen, ...)
label6.pack()
software.pack()
hardware.pack()
IDK.pack()您为每个小部件定义了一个自定义字体,但是您使用的是相同的自定义字体。如果您在稍后的日期决定更改字体,您将不得不在许多地方更改它。
相反,使用一个变量作为字体。此外,在字体的特定情况下,您可以利用tkinter极其强大的命名字体功能。您可以将字体创建为一个对象,稍后如果您更改该对象,那么所有的小部件都会自动更新。
例如
from tkinter import font
...
defaut_font = font.Font(family='Helvetica', size=15)
...
label6=Label(..., font=default_font)
software=Button(..., font=default_font)
hardware=Button(..., font=default_font)
...的面向对象特性。
创建GUI需要很多对象,这些对象必须从许多地方引用。对于这个问题,有几种解决方案:使用大量的全局变量,或者使用类将这些对象封装在一个地方。
这篇文章已经太长了,无法充分描述如何使用面向对象的技术。您可以在这个堆栈溢出回答:https://stackoverflow.com/a/17470842/7432中读到更多关于它的信息。
答案中的例子如下:
import tkinter as tk
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
<create the rest of your GUI here>
if __name__ == "__main__":
root = tk.Tk()
MainApplication(root).pack(side="top", fill="both", expand=True)
root.mainloop()尽管许多tkinter教程都是这样做的,但它们与PEP8和良好的编码实践背道而驰。我建议进口它,并给它一个简短的"tk“。然后,您需要用tk.作为每个小部件的前缀,而IMHO使代码更容易理解( Python的禅宗(PEP20)说“显式比隐式好”)
例如:
import tkinter as tk
...
root = tk.TK()
button = tk.Button(...)
...如果您正在创建故障排除指南,您可能需要考虑类似向导的设计,它将一系列屏幕与按钮捆绑在一起(例如: next,back)。
有关如何做到这一点的示例,请从以下堆栈溢出问题开始:在Tkinter中创建向导
https://codereview.stackexchange.com/questions/152324
复制相似问题