用tkinter合并两个窗口

时间:2016-10-25 14:08:07

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

我今天刚刚开始玩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()

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您可以使用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()