【问题标题】:How to make Tkinter button open in the same windows like in many other programs如何像在许多其他程序中一样使 Tkinter 按钮在相同的窗口中打开
【发布时间】:2019-03-17 14:11:12
【问题描述】:

我有一个程序计算器,按钮代码打开一个新窗口,但我希望按钮计算器在同一个窗口中运行,而不是两个单独的窗口,我该如何在同一个窗口中运行我的代码? 附言代码不是我的,仅供参考

from tkinter import *

root = Tk()
root.title("Math Lab")
root.geometry("1400x1000")
heading = Label(root, text = "Welcome to the MATH Lab", font=("Berlin Sans FB", 40, "bold"), fg= "steelblue").pack()
root.configure(background= "powder blue")


def calculator():
    w = Tk()
    w.geometry("1400x1000")

    def evaluate1():
        res.configure(text="Answer: " + str(eval(entry.get())))

    def evaluate(event):
        res.configure(text="Answer: " + str(eval(entry.get())))

    but1 = Button(w, text="Enter", width=10, height=3)
    but1.place(x=650, y=100)
    but1.config(command=evaluate1)

    Label(w, text="Your Expression:").pack()
    entry = Entry(w)
    entry.bind("<Return>", evaluate)
    entry.pack()
    res = Label(w)
    res.pack()
    w.mainloop()

but1=Button(root,text="Calculator",width = 10, height = 3)
but1.place(x=100, y=100)
but1.config(command = calculator)


root.mainloop()

有什么建议吗?

【问题讨论】:

  • 你为什么要发布不属于你的代码?向我们展示您的代码。

标签: python-3.x button tkinter


【解决方案1】:

您在这里请求一个新窗口:

    w = Tk()

替换为

    w = root

你可以走了。 (好吧,为了美观,您需要整理一些 x,y 偏移量。)

【讨论】:

    猜你喜欢
    • 2021-09-18
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 2015-10-09
    • 2021-12-03
    • 2019-06-30
    相关资源
    最近更新 更多