tkinter帧没有显示小部件

时间:2018-02-26 07:45:39

标签: python tkinter

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 个答案:

答案 0 :(得分: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 :(得分:1)

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

  1. 儿童到win2,而不是任何框架
  2. 被另一个框架frame3
  3. 拦截

    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

    注释掉第3行以查看lbl1btn1实际存在:

    #frame3 = Frame(win2,width=600,height=430,bg="red",relief=FLAT).pack(side=LEFT,fill=X)
    

答案 2 :(得分:-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()