相关疑难解决方法(0)

在tkinter中,为什么winfo_height()总是返回1?

这是最简单的例子.

#py3
from tkinter import *
tk = Tk()
canvas = Canvas(tk, width= 500 , height = 400)
canvas.winfo_height()
#In [4]: canvas.winfo_height()
#Out[4]: 1
Run Code Online (Sandbox Code Playgroud)

python tkinter

5
推荐指数
2
解决办法
5418
查看次数

如何在画布Python Tkinter中居中图像

我正在制作一个节目.获取图像并将其放入画布中.所以它就像一个照片浏览器.但我想将图像放在画布小部件的中心.但它似乎进入了左上角.为什么这样做?我怎样才能将它放在画布小部件的中心?

码:

from Tkinter import *
from PIL import ImageTk, Image
import os

class Application(Frame):
    def __init__(self, parent):
        Frame.__init__(self,parent)
        self.pack(fill=BOTH, expand=True)

        self.create_Menu()
        self.create_widgets()

    def create_Menu(self):
        self.menuBar = Menu(self)

        self.fileMenu = Menu(self.menuBar, tearoff=0)
        self.fileMenu.add_command(label="Open", command=self.getImage)
        self.fileMenu.add_separator()
        self.fileMenu.add_command(label="Exit", command=self.exitProgram)

        self.menuBar.add_cascade(label="File", menu=self.fileMenu)

        root.config(menu=self.menuBar)

    def create_widgets(self):
        self.viewWindow = Canvas(self, bg="white")
        self.viewWindow.pack(side=TOP, fill=BOTH, expand=True)

        def getImage(self):
        imageFile = Image.open("C:/Users/Public/Pictures/Sample Pictures/Desert.jpg")
        imageFile = ImageTk.PhotoImage(imageFile)

        self.viewWindow.image = imageFile
        self.viewWindow.create_image(0, 0, anchor=CENTER, image=imageFile, tags="bg_img")

    def exitProgram(self):
        os._exit(0)

root = Tk()
root.title("Photo Zone")
root.wm_state('zoomed')

app = Application(root)

root.mainloop()
Run Code Online (Sandbox Code Playgroud)

python canvas image tkinter python-imaging-library

5
推荐指数
1
解决办法
1万
查看次数

Python:检索ttk.Frame大小

我想要获得Frame的大小 - 但没有运气.

在下面的代码中 - 我得到了两个答案,"0"(标有***的行)

from tkinter import *
from tkinter import ttk

root = Tk()
w = '400'
h = '100'
root.geometry('{}x{}'.format(w, h))
root.configure(bg='lightgreen')

txt = StringVar()
txt.set("Hello")


testframe = ttk.Frame(root)
testframe.grid(row=0, column=1 )

label1 = ttk.Label(testframe, textvariable=txt)
label1.grid(row=0, column=0)

print(testframe["width"], testframe.cget("width"))   ***This line

root.mainloop()
Run Code Online (Sandbox Code Playgroud)

python tkinter

3
推荐指数
1
解决办法
225
查看次数

标签 统计

python ×3

tkinter ×3

canvas ×1

image ×1

python-imaging-library ×1