简体   繁体   中英

In python3 tkinter, the wigdet frame doesn't show in interface

I use the same format of frame but it doesn't show in the interface, hope someone could tell me the solution, thanks.

class Interface(Frame):
    def __init__(self,parent=None):
        Frame.__init__(self,parent)
        self.master.title("measurement")
        self.grid()
        # fix the size and parameters of widget
        self.master.geometry("700x400+100+50")
        self.master.Frame1 = Frame(self,relief=GROOVE,bg='white')
        self.master.Frame1.grid(column=1,row=9)
        self.can =Canvas(self, bg="ivory", width =200, height =150)
        self.master.canvas = Canvas(self.master, width=150, height=120, background='snow')
        ligne1=self.master.canvas.create_line(75, 0, 75, 120)

if __name__ == "__main__":
    window = Tk()
    window.resizable(False, False)

    Interface(window).mainloop()

I can't figure out why you have 2 Canvas 's, but the problem is that you aren't placing them on their respective parents. I cut out a lot of the code that seemed unnecessary and restructured your code to make it more logical:

class Interface(Frame):
    def __init__(self, parent):
        self.parent = parent
        super().__init__(self.parent)
        self.Frame1 = Frame(self, relief=GROOVE)
        self.Frame1.grid()

        self.canvas = Canvas(self.Frame1, bg="ivory", width=200, height=150)
        self.canvas.grid()

        self.canvas.create_line(75, 0, 75, 120)

if __name__ == "__main__":
    root = Tk()

    # Tk configurations are not relevant to
    # the Interface and should be done out here
    root.title('Measurement')
    root.geometry('700x400+100+50')
    root.resizable(False, False)

    Interface(root).pack()

    root.mainloop()

i think I don't really understand your problem, you don't see your frame because you don't have any widget in it, that's all

import tkinter as tk

class Interface(tk.Frame):
    def __init__(self,parent=None):
        tk.Frame.__init__(self,parent)
        self.master.title("measurement")
        self.grid(row=0, column=0)
        # fix the size and parameters of widget
        self.master.geometry("700x400+100+50")

        self.master.Frame1 = tk.Frame(self,relief='groove',bg='white')
        self.master.Frame1.grid(column=1,row=9)

        labelExemple =tk.Label(self.master.Frame1, text="Exemple")
        labelExemple.grid(row=0,column=0)

        self.can = tk.Canvas(self, bg="ivory", width =200, height =150)
        self.master.canvas = tk.Canvas(self.master, width=150, height=120, background='snow')
        self.ligne1=self.master.canvas.create_line(75, 0, 75, 120)


if __name__ == "__main__":
    window = tk.Tk()
    window.resizable(False, False)    
    Interface(window).mainloop()

PS : use import tkinter as tk instead of from tkinter import *

There are several problems with those few lines of code, almost all having to do with the way you're using grid:

  1. you aren't using the sticky option, so widgets won't expand to fill the space they are given
  2. you aren't setting the weight for any rows or columns, so tkinter doesn't know how to allocate unused space
  3. you aren't using grid or pack to put the canvases inside of frames, so the frames stay their default size of 1x1

The biggest problem is that you're trying to solve all of those problems at once. Layout problems are usually pretty simple to solve as long as you're only trying to solve one problem at a time.

Start by removing all of the widgets from Interface. Then, give that frame a distinctive background color and then try to make it fill the window (assuming that's ultimately what you want it to do). Also, remove the root.resizable(False, False) . It's rarely something a user would want (they like to be able to control their windows), plus it makes your job of debugging layout problems harder.

Once you get your instance of Interface to appear, add a single widget and make sure it appears too. Then add the next, and the next, adding one widget at a time and observing how it behaves.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM