【问题标题】:PYTHON // tkinter / How to grid a frame in a classPYTHON // tkinter / 如何在类中网格化框架(已解决)
【发布时间】:2022-10-24 23:10:43
【问题描述】:

大家好 !

我正在寻找解决方案从一个类的框架中制作一个网格.

以下代码不允许我使用画布在网格内放置 2 个对象(此处为描述和按钮)。

显然我应该将网格直接应用于框架,但如果我这样做; 该框不考虑尺寸和窗口的最小化。

目的是制作一个浮动工具栏(如史诗笔)!

因此,如果有人有解决方案来解决这个问题,那对你来说真的很好谢谢 =)

这是带有画布的代码制作一个盒子我的物品:

import tkinter as tk

from tkinter import *
from tkinter.ttk import *
from tkinter import filedialog
from tkinter.filedialog import askopenfile
from PIL import Image, ImageTk


#OPENING | https://www.youtube.com/watch?v=_85LaeTCtV8 :3
def main():
    root = tk.Tk()
    app = w1(root)
    root.mainloop()


#NEW WINDOW !INSIDE! THE MAIN ONE
def new_window(self):
    self.newWindow = tk.Toplevel(self.master)
    self.app = w2(self.newWindow)


#UPLOADED PICTURE
def open_file(self):

    global f_path, pic, h, w

    f_type = [('JPEG Files', '*.jpg'), ('PNG Files', '*.png')]
    f_path = filedialog.askopenfilename(filetype=f_type)
    pic = ImageTk.PhotoImage(file=f_path)

    h = pic.height()
    w = pic.width()

    new_window(self)


#ENDING | https://www.youtube.com/watch?v=P4q6dVdvF40 :3
def close_windows(self):
    self.master.destroy()




#MAIN's WINDOW
class w1:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
################################################################# PROBLEM!
#Create a Canvas for grid
        canvas = tk.Canvas(
                   width=300, height=300
                   )

        canvas.grid(
            columnspan=30, rowspan=30,
            )

################################################################# PROBLEM!

#Button to browse the picture
        self.browser_txt = tk.StringVar()
        self.browse = tk.Button(

            self.frame,
            textvariable=self.browser_txt,            
            command=lambda: open_file(self),

            bg="#20bebe",
            fg="white",

            font="Tahoma",
            )


        self.browser_txt.set( "BROWSE")

        self.browse.grid(
            column=14, row=5
        )


#INSTRUCTIONS

        self.instructions = Label(self.frame,
                                  text="jpg or png",
                                  font="Tahoma",
                                  anchor=CENTER, justify='center'
                                  )
        self.instructions.grid(
            column=14, row=6
        )


#Packman
################################################################# PROBLEM!
        canvas.pack()
################################################################# PROBLEM!
        self.frame.pack()



#PICTURE's WINDOW
class w2:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)

#Define the picture
        self.picture = Image.open(f_path) 
        canvas = Canvas(self.frame, width=w, height=h)
        canvas.create_image(1, 1, anchor=NW, image=pic)

#Packman
        canvas.pack()
        self.frame.pack()



if __name__ == '__main__':
    main()

这是直接应用于框架的网格代码在没有盒子尺寸的情况下运行;

#_ _  _ ____ ___ ____ _    _    ____ ___ _ ____ _  _
#| |\ | [__   |  |__| |    |    |__|  |  | |  | |\ |
#| | \| ___]  |  |  | |___ |___ |  |  |  | |__| | \|


#ALEXANDRIA
import tkinter as tk

from tkinter import *
from tkinter.ttk import *

from tkinter import filedialog
from tkinter.filedialog import askopenfile

from PIL import Image, ImageTk






#___  ____ _ _ _ ____ ____    ___  _    ____ _  _ ___ 
#|__] |  | | | | |___ |__/    |__] |    |__| |\ |  |  
#|    |__| |_|_| |___ |  \    |    |___ |  | | \|  |
                                      

#OPENING | https://www.youtube.com/watch?v=_85LaeTCtV8 :3
def main():
    root = tk.Tk()
    app = w1(root)
    root.mainloop()


#NEW WINDOW !INSIDE! THE MAIN ONE
def new_window(self):
    self.newWindow = tk.Toplevel(self.master)
    self.app = w2(self.newWindow)


#UPLOADED PICTURE
def open_file(self):

    global f_path, pic, h, w

    f_type = [('JPEG Files', '*.jpg'), ('PNG Files', '*.png')]
    f_path = filedialog.askopenfilename(filetype=f_type)
    pic = ImageTk.PhotoImage(file=f_path)

    h = pic.height()
    w = pic.width()

    new_window(self)


#ENDING | https://www.youtube.com/watch?v=P4q6dVdvF40 :3
def close_windows(self):
    self.master.destroy()






#___  ____ ____ ____ ____ ____ _  _ 
#|__] |__/ |  | | __ |__/ |__| |\/| 
#|    |  \ |__| |__] |  \ |  | |  | 


#MAIN's WINDOW
#Manage objects in the main window
class w1:
    def __init__(self, master):
        self.master = master

################################################################# PROBLEM!
        self.frame = tk.Frame(self.master, width=300, height=300)
        self.frame.grid(columnspan=30, rowspan=30)
################################################################# PROBLEM!
        
#Button to browse the picture
        self.browser_txt = tk.StringVar()
        self.browse = tk.Button(

            self.frame,
            textvariable=self.browser_txt,            
            command=lambda: open_file(self),

            bg="#20bebe",
            fg="white",

            font="Tahoma",

         
            )


        self.browser_txt.set( "BROWSE")

        self.browse.grid(
            column=0, row=0
        )


#INSTRUCTIONS

        self.instructions = Label(self.frame,
                                  text="jpg or png",
                                  font="Tahoma",
                                  anchor=CENTER, justify='center'
                                  )
        self.instructions.grid(
            column=0, row=1
        )


#Final flash
        self.frame.pack()



#PICTURE's WINDOW
#Manage objects in the picture's window
class w2:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)

#Define the picture
        self.picture = Image.open(f_path) 
        canvas = Canvas(self.frame, width=w, height=h)
        canvas.create_image(1, 1, anchor=NW, image=pic)


#Final flash
        canvas.pack()
        self.frame.pack()






#____ ____ ____ _  _ ____ ___    _    ____ _  _ _  _ ____ _  _
#|__/ |  | |    |_/  |___  |     |    |__| |  | |\ | |    |__| 
#|  \ |__| |___ | \_ |___  |     |___ |  | |__| | \| |___ |  |


if __name__ == '__main__':
    main()

对于这个故事,它是一个代码免费的开源工具放一个图片叠加应用对于那些不得不无图层再现 3D(如与超人类,或者想要再现人在 RPG 或任何其他功能中......

事实上,这个项目几乎完成了(比这更先进),但我想先清理它,我有点卡在这个基本问题上,所以谢谢帮助 ^^'

【问题讨论】:

    标签: python class grid frame tkinter-canvas


    【解决方案1】:

    问题是网格谁不是最好的处理方式。

    我不得不使用盒()对于我所有的对象(框架, 指示,按钮) 然后地方()。

    这是我的重写代码的示例班级窗户 1;

    class w1:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master,width=100, height=400)
        self.frame.pack(fill=None, expand=False)
        self.frame.place()
    
        #INSTRUCTIONS
    
        self.instructions = Label(self.frame,
                                  text="jpg or png",
                                  font="Tahoma",
                                  anchor=CENTER, justify='center'
                                  )
        self.instructions.pack()
        self.instructions.place(rely=0.1, anchor=NW)
    

    这是一个关于地方(); https://www.tutorialspoint.com/python/tk_place.htm

    【讨论】:

      猜你喜欢
      • 2014-12-16
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      • 2015-10-03
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多