【问题标题】:Combine two windows with tkinter将两个窗口与 tkinter 结合起来
【发布时间】:2023-04-06 13:51:01
【问题描述】:

我今天刚开始玩 tkinter,我有两段代码作为示例一直在玩,但是我正在努力将这些代码结合起来,任何人都可以建议。我想在主窗口中显示时钟。

import tkinter
from tkinter import *
import sys
import time


root = Tk()
root.title('Logging')
Label(text='Time logging').pack(side=TOP,padx=100,pady=100)

entry = Entry(root, width=25)
entry.pack(side=TOP,padx=25,pady=25)

def onok():
    x, y = entry.get().split('x')
    for row in range(int(y)):
        for col in range(int(x)):
            print((col, row))

Button(root, text='Log Time', command=onok).pack(side=LEFT)
Button(root, text='CLOSE').pack(side= RIGHT)

def tick():
    global time1
    # get the current local time from the PC
    time2 = time.strftime('%H:%M:%S')
    # if time string has changed, update it
    if time2 != time1:
        time1 = time2
        clock.config(text=time2)
        # calls itself every 200 milliseconds
        # to update the time display as needed
        # could use >200 ms, but display gets jerky
    clock.after(200, tick)

root = Tk()
time1 = ''

status = Label(root, text="v1.0", bd=1, relief=SUNKEN, anchor=W)
status.grid(row=10, column=10)

clock = Label(root, font=('times', 20, 'bold'), bg='green')
clock.grid(row=0, column=1) 

tick()
root.mainloop()

【问题讨论】:

    标签: python python-2.7 python-3.x tkinter


    【解决方案1】:

    您可以使用Frame 对时钟小部件进行分组,并在此框架内使用grid。你可以把框架放在主窗口中。 (而且你不需要第二个Tk()

    我把它放在最上面,但你可以选择其他地方。

    import tkinter as tk
    import time
    
    # --- functions ---
    
    def on_ok():
        x, y = entry.get().split('x')
        for row in range(int(y)):
            for col in range(int(x)):
                print((col, row))
    
    def tick():
        global time1
    
        # get the current local time from the PC
        time2 = time.strftime('%H:%M:%S')
    
        # if time string has changed, update it
        if time2 != time1:
            time1 = time2
            clock.config(text=time2)
            # calls itself every 200 milliseconds
            # to update the time display as needed
            # could use >200 ms, but display gets jerky
        clock.after(200, tick)
    
    # --- main window ---
    
    time1 = ''
    
    root = tk.Tk()
    root.title('Logging')
    
    # add frame in main window (root)
    
    other = tk.Frame(root)
    other.pack()
    
    # put widgets in frame (other)
    
    status = tk.Label(other, text="v1.0", bd=1, relief=tk.SUNKEN, anchor=tk.W)
    status.grid(row=10, column=10)
    
    clock = tk.Label(other, font=('times', 20, 'bold'), bg='green')
    clock.grid(row=0, column=1) 
    
    # put other widget directly in main widnow (root)
    
    tk.Label(root, text='Time logging').pack(side=tk.TOP, padx=100, pady=100)
    
    entry = tk.Entry(root, width=25)
    entry.pack(side=tk.TOP, padx=25, pady=25)
    
    tk.Button(root, text='Log Time', command=on_ok).pack(side=tk.LEFT)
    tk.Button(root, text='CLOSE', command=root.destroy).pack(side= tk.RIGHT)
    
    tick()
    
    # --- start ---
    
    root.mainloop()
    

    【讨论】:

      【解决方案2】:

      您有两个不同的根窗口。在顶部使用一个 root = Tk() 行,您应该将它们放在同一页面上。

      【讨论】:

      • 我是这么想的,但是删除一个会给我以下错误;
      • 是的,你不能在 root 的子节点上同时使用 .pack 和 .grid。尝试将所有包更改为网格或将所有网格更改为打包,看看我的意思。
      • 检查this。它解释了使用网格的所有细节。如果您注意到,中间有一个绿色的大警告,解释了我的上述评论。
      • 谢谢,这个页面真的很有帮助
      猜你喜欢
      • 2021-09-28
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      • 2011-08-02
      • 2021-11-23
      • 2021-09-05
      • 1970-01-01
      • 2021-11-21
      相关资源
      最近更新 更多