m0_63392446 2022-05-28 16:42 采纳率: 100%
浏览 175
已结题

tkinter出现两个窗口怎么合并(初学者)

问题遇到的现象和发生背景

tkinter出现两个窗口怎么合并

问题相关代码,请勿粘贴截图

from tkinter import*

root = Tk()
root.title('环境')
root.minsize(200,100)

Label(root,text="温度:0 ℃").grid(row=0,column=0)
Label(root,text="湿度:0 %RH").grid(row=0,column=4)
Label(root,text="光照:0 Lux").grid(row=4,column=0)
Label(root,text="土壤:0 ADC").grid(row=4,column=4)
root.mainloop()
import time
import tkinter as tk

class App:
def init(self):
self.root = tk.Tk()
self.label = tk.Label(text="")
self.label.pack()
self.update_clock()
self.root.mainloop()

def update_clock(self):
    now = time.strftime("%H:%M:%S")
    self.label.configure(text = now)
    self.root.after(1000,self.update_clock)

app = App()

运行结果及报错内容

出现两个窗口

我的解答思路和尝试过的方法
我想要达到的结果

img

img

  • 写回答

1条回答 默认 最新

  • 请叫我问哥 Python领域新星创作者 2022-05-28 17:10
    关注

    这样就合并了
    布局还要再调整

    import time
    import tkinter as tk
    
    class App:
        def __init__(self):
            self.root = tk.Tk()
            self.root.title('环境')
            self.root.minsize(200,100)
            tk.Label(self.root,text="温度:0 ℃").grid(row=0,column=0)
            tk.Label(self.root,text="湿度:0 %RH").grid(row=0,column=4)
            tk.Label(self.root,text="光照:0 Lux").grid(row=4,column=0)
            tk.Label(self.root,text="土壤:0 ADC").grid(row=4,column=4)
            self.label = tk.Label(text="")
            self.label.grid(row=5)
            self.update_clock()
            self.root.mainloop()
        
        def update_clock(self):
            now = time.strftime("%H:%M:%S")
            self.label.configure(text = now)
            self.root.after(1000,self.update_clock)
    
    app = App()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 6月5日
  • 已采纳回答 5月28日
  • 创建了问题 5月28日

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM