したがって、以下のコードは私の実際のコードの短縮バージョンです。フレーム内に配置されたフレーム内にウィジェットを表示するときに問題が発生します。
したがって、基本的に以下のコードはログイン メニューであり、ボタンをクリックするとフレームが別のフレームに変わります。この場合login_frame
にoverview_frame
. overview_frame
i の中にと呼ばれる別のフレームがありtitleFrame
、このフレームは に配置されますoverview_frame
。
titleFrame
この場合、配置方法を使用titleLabel
してウィジェットをこのインスタンス.grid()
に配置したいときに問題が発生します。
私が作成した他の tkinter アプリケーションでこれを実行しましたが、これを数時間実行しようとしましたが、結論には至りませんでした。これは私が得続けるエラーです:
Traceback (most recent call last):
File "C:\Users\nasto\Documents\Company\rfr.py", line 67, in <module>
app = frame_store()
File "C:\Users\nasto\Documents\Company\rfr.py", line 16, in __init__
frame = n_frame(parent=container, controller=self)
File "C:\Users\nasto\Documents\Company\rfr.py", line 57, in __init__
titleLabel = tk.Label(titleFrame, text='TITLE').grid(row=0,column=0, sticky='nsew', padx=2, pady=2)
File "C:\Users\nasto\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 2220, in grid_configure
+ self._options(cnf, kw))
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack
このコードと私が作成した他のアプリケーションとの唯一の違いはframe_store
クラスです。使用された 1 行のコードがありますが、.Pack()
これを削除するlogin_frame
とtitleFrame
表示されず、ウィジェットは直接overview_frame
. .Pack()
すべてのフレームがまだ表示されていることを確認しながら、または使用中に を配置するtitleLabel
には、どうすればよいでしょうか。個人的には、e // に接続されているものと関係があると思いますが、それをいじりながら機能する結果が得られていません。titleFrame
.grid()
titleFrame
self
import tkinter as tk
import hashlib
import sqlite3
class frame_store(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
'''container is used so that all the frames can be stacked, from there we can call other frames within the container to the top, to the current active frame'''
container = tk.Frame(self)
container.pack(side='top', fill='both', expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for n_frame in (login_frame, overview_frame, accounting_frame):
pageName = n_frame.__name__
frame = n_frame(parent=container, controller=self)
self.frames[pageName] = frame
frame.grid(row=0, column=0, sticky='nsew')
self.show_frames('login_frame')
def show_frames(self, pageName):
frame = self.frames[pageName]
self.update_idletasks()
self.state('zoomed')
self.title('A2 Project')
frame.tkraise()
class login_frame (tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
loginButton = tk.Button(self, text='Login', command = self.no_credentials).grid(row=3,column=0)
self.controller.bind('<Control-n>', lambda event: self.no_credentials())
def no_credentials(self):
self.controller.show_frames('overview_frame')
def clear(self):
self.__userID.set('')
self.__password.set('')
def exit(self):
app.destroy()
class overview_frame(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
self.config(background='#000000')
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
titleFrame = tk.Frame(self, background='#f8f8ff', bd=1, relief='sunken').grid(row=0, column=0, sticky='nsew', padx=2, pady=2)
titleLabel = tk.Label(titleFrame, text='TITLE').grid(row=0,column=0, sticky='nsew', padx=2, pady=2)
class accounting_frame(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.controller = controller
pass
if __name__ == "__main__":
app = frame_store()
app.mainloop()
あなたの問題が原因にある作成あなたのフレームをと整合させる単一のコード行でそれを。
書くとき:
titleFrame = tk.Frame(self, relief='sunken').grid(row=0, column=0)
titleFrame
作成したばかり.grid()
ではなく、メソッドの出力tk.Frame
です。したがって、将来のウィジェットの親として使用することはできません。
ウィジェットへの参照を保持するには、ウィジェットの作成とウィジェットの配置を分離する必要があります。
titleFrame = tk.Frame(self, background='#f8f8ff', bd=1, relief='sunken')
titleFrame.grid(row=0, column=0, sticky='nsew', padx=2, pady=2)
titleLabel = tk.Label(titleFrame, text='TITLE')
titleLabel.grid(row=0,column=0, sticky='nsew', padx=2, pady=2)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加