将小部件放入框架内的框架时出错。 Tkinter - Python3

时间:2017-04-06 23:57:28

标签: python tkinter

所以下面的代码是我实际代码的简化版本。当我将一个小部件显示在放置在框架内的框架内时,我遇到了问题。

所以,基本上下面的代码是一个登录菜单,当点击一个按钮时,框架会变为另一个框架。在这种情况下login_frameoverview_frame。在overview_frame内我有另一个名为titleFrame的框架,此框架位于overview_frame

现在出现问题的时候,我想在titleFrame titleLabel中使用.grid()展示位置方法将小部件放入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

我已经使用我创建的其他tkinter应用程序完成了这项工作,我一直试图将这个问题解决了好几个小时,现在还没有得出结论。这是我不断得到的错误:

frame_store

现在,此代码与我所做的其他应用程序之间的唯一区别是.Pack()类。其中有一行代码使用了login_frame但是在删除时,titleFrameoverview_frame没有显示,小部件直接转到.Pack()。如何在确保所有帧仍然可见的同时摆脱titleLabel,或者在使用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() 的关联有关,但在弄乱我的时候我没有得到有效的结果。

$(document).ready(function() {   /* executes after page been loaded */

  $(".myinputs").change(function() { 
    $(this).removeClass('err').addClass('wider');
  });

});

1 个答案:

答案 0 :(得分:1)

您的问题是由于创建您的框架而对齐只需一行代码。

写作时:

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)