首页 文章

tkinter帧没有显示小部件

提问于
浏览
-3
from tkinter import *
import tkinter.messagebox


def message():
    text='''sfjkasjdfkjasdfjsdjfjsdlfjasd
            fjsdkfjksadjfsajdjfl    
            sdfasdjflsjdlfsldjflsjd'''
    tkinter.messagebox.showinfo("showing",text)


def _price_inputs():
    win2 = Tk()
    win2.title("Transactions for the project Botique")
    win2.geometry("1600x800+0+0")
    win2.configure(bg="black")


    framex = Frame(win2,width=1600,bg="RoyalBlue4",height=100,relief=GROOVE).pack(side=TOP)
    frame1 = Frame(win2,width=1000, height=400,bg="white", relief=SUNKEN).pack(side=RIGHT,fill=Y)
    frame2 = Frame(win2, width=775,height=100,bg="white", relief=FLAT).pack(side=BOTTOM)
    frame3 = Frame(win2,width=600,height=430,bg="gray",relief=FLAT).pack(side=LEFT,fill=X)

    #framex == heading
    #frame1 == showing the infos 
    #frame2 == bottom_infos
    #frme3 == adding the buttons and widgets

    #==++++===========================title=============================

    lbl1 = Label(framex,font=("arial", 30, "bold"),bg="powder blue",fg="green",text="Hello this is the title of the page",bd=10,relief=GROOVE).pack(side=TOP)

    btn1 = Button(frame1,font=("arial",20,"bold"),bg="powder blue",fg="white",text="click me").pack()


    win2.mainloop()

我正在尝试使用tkinter创建gui . 我正在使用python3.6我使用tkinter制作帧,现在当我尝试添加按钮,标签等时,它不会在输出屏幕中显示按钮或标签 .

以及如何使用pack将包用于框架和网格用于该框架中的小部件 .

3 回答

  • 1

    您没有调用已定义属性的函数 . 只需通过最后添加 _price_inputs() 代码来调用函数:

    from tkinter import *
    import tkinter.messagebox
    
    
    def message():
        text='''sfjkasjdfkjasdfjsdjfjsdlfjasd
                fjsdkfjksadjfsajdjfl    
                sdfasdjflsjdlfsldjflsjd'''
        tkinter.messagebox.showinfo("showing",text)
    
    
    def _price_inputs():
        win2 = Tk()
        win2.title("Transactions for the project Botique")
        win2.geometry("1600x800+0+0")
        win2.configure(bg="black")
    
    
        framex = Frame(win2,width=1600,bg="RoyalBlue4",height=100,relief=GROOVE).pack(side=TOP)
        frame1 = Frame(win2,width=1000, height=400,bg="white", relief=SUNKEN).pack(side=RIGHT,fill=Y)
        frame2 = Frame(win2, width=775,height=100,bg="white", relief=FLAT).pack(side=BOTTOM)
        frame3 = Frame(win2,width=600,height=430,bg="gray",relief=FLAT).pack(side=LEFT,fill=X)
    
        #framex == heading
        #frame1 == showing the infos 
        #frame2 == bottom_infos
        #frme3 == adding the buttons and widgets
    
        #==++++===========================title=============================
    
        lbl1 = Label(framex,font=("arial", 30, "bold"),bg="powder blue",fg="green",text="Hello this is the title of the page",bd=10,relief=GROOVE).pack(side=TOP)
    
        btn1 = Button(frame1,font=("arial",20,"bold"),bg="powder blue",fg="white",text="click me").pack()
    
    
        win2.mainloop()
    _price_inputs()
    
  • 1

    您无法看到新项目 lbl1btn1 ,因为它们是:

    • 孩子到 win2 ,不是任何帧

    • 被另一帧阻止, frame3


    1

    lbl1btn1win2 的子项,因为将None作为第一个位置参数传递,或者默认情况下,窗口小部件的父项被指定为Tk实例 .

    lbl1btn1 使用父参数实例化为 None ,因为:

    framex = Frame(win2,width=1600,bg="RoyalBlue4",height=100,relief=GROOVE).pack(side=TOP)
    ...
    frame1 = Frame(win2,width=600,height=430,bg="red",relief=FLAT).pack(side=LEFT,fill=X)
    

    线条与以下相同:

    Frame(win2,width=1600,bg="RoyalBlue4",height=100,relief=GROOVE).pack(side=TOP)
    framex = None
    ...
    Frame(win2,width=600,height=430,bg="red",relief=FLAT).pack(side=LEFT,fill=X)
    frame1 = None
    

    因为 framexframe3 are the return of the method pack which is always None .

    可以通过将几何管理器行与窗口小部件实例化行分开来解决此问题:

    framex = Frame(win2,width=1600,bg="RoyalBlue4",height=100,relief=GROOVE)
    framex.pack(side=TOP)
    ...
    frame1 = Frame(win2,width=600,height=430,bg="red",relief=FLAT)
    frame1.pack(side=LEFT,fill=X)
    

    2

    注释掉frame3行,看看 lbl1btn1 确实存在:

    #frame3 = Frame(win2,width=600,height=430,bg="red",relief=FLAT).pack(side=LEFT,fill=X)
    
  • -2
    from tkinter import *
    import tkinter.messagebox
    
    
    """def message():
        text='''sfjkasjdfkjasdfjsdjfjsdlfjasd
                fjsdkfjksadjfsajdjfl    
                sdfasdjflsjdlfsldjflsjd'''
        tkinter.messagebox.showinfo("showing",text)"""
    
    
    def _price_inputs():
        win2 = Tk()
        win2.title("Transactions for the project Botique")
        win2.geometry("1600x800+0+0")
        win2.configure(bg="white")
    
    
        framex = Frame(win2,width=1600,bg="RoyalBlue4",height=100,relief=GROOVE).pack(side=TOP)
        #frame1 = Frame(win2,width=1000, height=400,bg="white", relief=SUNKEN).pack(side=RIGHT,fill=Y)
        frame2 = Frame(win2, width=775,height=100,bg="black", relief=FLAT).pack(side=BOTTOM)
        frame3 = Frame(win2,width=800,height=450,bg="gray",relief=FLAT).pack(side=LEFT,fill=X)
    
        #framex == heading
        #frame1 == showing the infos 
        #frame2 == bottom_infos
        #frme3 == adding the buttons and widgets
    
        #==++++===========================title=============================
    
        lbl1 = Label(framex,font=("arial", 30, "bold"),bg="black",fg="green",text="Hello this is the title of the page",bd=10,relief=GROOVE).pack(side=TOP)
    
        btn1 = Button(frame3,font=("arial",15,"bold"),bd=8,bg="black",fg="white",text="before 60 hrs",relief=GROOVE).pack(side=BOTTOM)
        btn2 = Button(frame3,font=("arial",15,"bold"),bd=8,bg="black",fg="white",text="full_stock",relief=GROOVE).pack(side=BOTTOM)
        btn3 = Button(frame3,font=("arial",15,"bold"),bd=8,bg="black",fg="white",text="delivery_report",relief=GROOVE).pack(side=BOTTOM)
    
        before = IntVar()
        stock_full = IntVar()
        delivery_report = IntVar()
    
        btn4 = Button(win2,font=("arial",15,"bold"),bd=8,bg="black",fg="white",text="hello",relief=GROOVE).pack(side=BOTTOM)
    
        '''import Tkinter as tk
    import ImageTk
    
    FILENAME = 'image.png'
    root = tk.Tk()
    canvas = tk.Canvas(root, width=250, height=250)
    canvas.pack()
    tk_img = ImageTk.PhotoImage(file = FILENAME)
    canvas.create_image(125, 125, image=tk_img)
    quit_button = tk.Button(root, text = "Quit", command = root.quit, anchor = 'w',
                        width = 10, activebackground = "#33B5E5")
    quit_button_window = canvas.create_window(10, 10, anchor='nw', window=quit_button)    
    root.mainloop()
    '''
        win2.mainloop()
    

相关问题