Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何根据python上tkinter中的用户输入生成弹出消息?_Python_Loops_Tkinter_Popupwindow - Fatal编程技术网

如何根据python上tkinter中的用户输入生成弹出消息?

如何根据python上tkinter中的用户输入生成弹出消息?,python,loops,tkinter,popupwindow,Python,Loops,Tkinter,Popupwindow,我正在尝试创建一个系统,用户可以使用两个滑块输入当前温度和所需温度。当按下按钮“Set”确认两个温度时,根据用户输入显示一条弹出消息 如果所需温度高于当前温度, 此弹出消息应显示:“打开加热器?” 如果所需温度低于当前温度, 此弹出消息应显示:“打开冷却器?” 我曾尝试生成此代码,但在单击“Set”按钮时,我的代码似乎没有生成任何内容。任何帮助都将不胜感激 class StartPage(tk.Frame): def __init__(self, parent, controlle

我正在尝试创建一个系统,用户可以使用两个滑块输入当前温度和所需温度。当按下按钮
“Set”
确认两个温度时,根据用户输入显示一条弹出消息

  • 如果所需温度高于当前温度, 此弹出消息应显示:
    “打开加热器?”
  • 如果所需温度低于当前温度, 此弹出消息应显示:“
    打开冷却器?”
我曾尝试生成此代码,但在单击
“Set”
按钮时,我的代码似乎没有生成任何内容。任何帮助都将不胜感激

class StartPage(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)

        label1=ttk.Label(self,text="Smart Thermostat",font=LARGE_FONT)
        label1.pack(pady=10, padx=10)

        label2 = ttk.Label(self, text="Current Temperature:",font=MEDIUM_FONT)
        label2.pack(pady=10, padx=10)

        slider1 = tk.Scale(self, from_=10, to = 30, orient=HORIZONTAL)
        slider1.pack()

        label3 = ttk.Label(self, text="Set to:",font=MEDIUM_FONT)
        label3.pack(pady=10, padx=10)

        slider2 = tk.Scale(self, from_=18, to = 25, orient=HORIZONTAL)
        slider2.pack()

        def popupmsg1(msg):
            popup1=tk.Tk()
            popup1.wm_title("!")
            label4 = ttk.Label(popup1, text="Turn heater on?", font = MEDIUM_FONT)
            label4.pack(side = "top", fill = "x", pady=10)
            button2=ttk.Button(popup1, text="Okay", command = popup1.destroy)
            button2.pack()
            popup1.mainloop()

        def popupmsg2(msg):
            popup2=tk.Tk()
            popup2.wm_title("!")
            label5 = ttk.Label(popup2, text="Turn cooler on?", font = MEDIUM_FONT)
            label5.pack(side = "top", fill = "x", pady=10)
            button3=ttk.Button(popup2, text="Okay", command = popup2.destroy)
            button3.pack()
            popup2.mainloop()    

        def popupmsg():
            temp=int(slider2.get())
            need=int(slider1.get())
            if temp<need:
                popup1=tk.Tk()
            else:
                popup2=tk.Tk()


        button1=tk.Button(self, text="Set", command= lambda: popupmsg)
        button1.pack(pady=10, padx=10)
class起始页(tk.Frame):
定义初始化(自、父、控制器):
tk.Frame.\uuuu init\uuuuu(自,父)
label1=ttk.Label(self,text=“智能恒温器”,font=LARGE\u font)
标签1.包装(pady=10,padx=10)
label2=ttk.Label(self,text=“当前温度:”,font=MEDIUM\u font)
标签2.包装(pady=10,padx=10)
滑块1=传统刻度(自、自=10、至=30、方向=水平)
滑块1.pack()
label3=ttk.Label(self,text=“设置为:”,font=MEDIUM\u font)
标签3.包装(pady=10,padx=10)
滑块2=传统刻度(自、自=18、至=25、方向=水平)
滑块2.pack()
def popupmsg1(消息):
popup1=tk.tk()
popup1.wm_标题(“!”)
label4=ttk.Label(popup1,text=“打开加热器?”,font=MEDIUM\u font)
标签4.pack(side=“top”,fill=“x”,pady=10)
button2=ttk.Button(popup1,text=“好”,command=popup1.destroy)
按钮2.pack()
popup1.mainloop()
def popupmsg2(消息):
popup2=tk.tk()
popup2.wm_标题(“!”)
label5=ttk.Label(popup2,text=“打开冷却器?”,font=MEDIUM\u font)
标签5.pack(side=“top”,fill=“x”,pady=10)
button3=ttk.Button(popup2,text=“好”,command=popup2.destroy)
按钮3.pack()
popup2.mainloop()
def popupmsg():
temp=int(slider2.get())
need=int(slider1.get())

如果临时您应该能够使用以下信息创建消息/对话框:

 # Python 3
 from tkinter import messagebox

 # Python 2
 import tkMessageBox as messagebox

 if case 1:
      messagebox.showinfo("title 1", "message 1")
 else:
      messagebox.showinfo("title 2", "message 2")
您通常只为真正自定义的信息框/窗口生成自定义窗口,这些信息框/窗口具有自己的逻辑。对于简单消息,使用内置消息框就足够了。即使这样,您也应该只使用
Toplevel
,而不是生成全新的
tk.tk
实例。Tk只是一个大的mainloop(因此使用.mainloop()…),它处理其中的事件

你如何选择向用户显示消息是一个非常开放的过程,你甚至可以制作一个标签来更新文本并适当地显示/隐藏文本等等


如果你想做自定义字体等它看起来像。。。没有深入挖掘你所有的代码。。。实际上,您必须选择顶级/小部件路线。

下次您提出问题时,请仅包括相关部分。如果你把整个图书馆都倒在这里,我们帮不了你:)我真的很抱歉,我是新来的。谢谢你的建议,非常感谢:)请为未来的读者提供一个参考。谢谢你的建议,我在上面提到我是新来的,但我再次为没有达到标准表示歉意。“即使这样,你也应该这么做”:产生多个
Tk
实例是非常糟糕的做法。@Coal_uuu同意。。。考虑到所提及的其他/事实,认为应该暗示。尽管如此,为了清晰起见,我还是会更新的。谢谢你,我会试试的!非常感谢您的帮助:)