2016-10-25 20 views
1

今日はtkinterで遊んだばかりですが、私は2つのコードを持っていますが、例として演奏していますが、これらを組み合わせるのは苦労しています。メインウィンドウに表示するようにクロックしたいと思います。2つのウィンドウを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() 

答えて

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() 
+0

ありがとう、これは素晴らしい作品です – Tom

0

2つの異なるルートウィンドウがあります。一番上にroot = Tk()の行を1つ使用し、同じページにそれらを置く必要があります。

+0

私はこれを考えましたが、削除すると次のエラーが表示されます。 – Tom

+1

内部でジオメトリマネージャグリッドを使用できません。 – Tom

+1

はい、ルートの子に.packと.gridを同時に使用することはできません。すべてのパックをグリッドまたはすべてのグリッドに変更して、パックして、意味を確認してみてください。 – B3Caballero

関連する問題