Python 用tkinter组合两个窗口
我今天刚开始玩tkinter,我有两段代码作为示例,但是我很难将它们结合起来,任何人都可以提供建议。我想在主窗口中显示时钟Python 用tkinter组合两个窗口,python,python-2.7,python-3.x,tkinter,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) e
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()
您有两个不同的根窗口。在顶部使用一行root=Tk(),您应该将它们放在同一页上。您有两个不同的根窗口。在顶部使用一个root=Tk()行,您应该将它们放在同一页上。您可以使用
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()
您可以使用
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()
我是这么想的,但是去掉一个会给我以下的错误;是的,您不能在root的子级上同时使用.pack和.grid。试着把你所有的背包换成格子,或者把你所有的格子换成格子,看看我的意思。它解释了使用网格的所有细节。如果你注意到,在绿色的中间有一个大的警告来解释我上面的评论。谢谢,这个页面真的很有用,我想,但是删除一个给了我以下的错误。是的,您不能在root的子级上同时使用.pack和.grid。试着把你所有的背包换成格子,或者把你所有的格子换成格子,看看我的意思。它解释了使用网格的所有细节。如果你注意到,在绿色的中间有一个大的警告,解释我上面的评论。谢谢,这个页面真的很有用。