当我声明大小限制时,为什么我的 tkinter 顶层很大?

我知道这可能是一个新问题,但我还没有找到答案。这是我的代码片段,其中有一个根窗口,其中包含一个用于打开顶级的按钮。顶层从文本文件中随机抽取一行,作为一种想法生成器。


import random, fileinput

import tkinter as tk

from tkinter import *


root = tk.Tk()

root.title('Daydreamer')

#fname should be the file name of the image in working directory

fname = "bg.gif"

bg_image = tk.PhotoImage(file=fname)


#get width and height of image

w = bg_image.width()

h = bg_image.height()    


#size window correctly

    root.geometry("500x400")

    cv = tk.Canvas(width=w, height=h)

    cv.pack(side='top', fill='both', expand='yes')

    cv.create_image(0,0,image=bg_image,anchor='nw')

    

    #add a frame for text

    mainframe=tk.Frame(root)

    

    #new window for inspirations

    def inspirations():

        top = Toplevel(root)

        top.geometry=("100x100")

        top.title("Inspiration")

        def idea():

            textidea=None

            for line in fileinput.input('textlist.txt'):

                if random.randrange(fileinput.lineno())==0:

                    textidea=line

            entrytext=tk.Text(top)

            entrytext.insert(INSERT, textidea)

            entrytext.insert(END, "Or press the Inspire Me button again for another idea!")

            entrytext.pack()

        idea()

            

 


           top.mainloop()

    

   

 

   

    #add buttons

    btn1 = tk.Button(cv, text="Inspire Me", command=inspirations)

    btn1.pack(side='left', padx=10, pady=5, anchor='sw')

    

    root.mainloop()

问题是,顶级总是绝对巨大(比我的根窗口大),对于其中显示的少量内容来说,这看起来非常愚蠢。我在这里错过了一些非常微小和愚蠢的事情吗?非常感谢帮助。


Smart猫小萌
浏览 53回答 1
1回答

慕森卡

问题是您没有调用该geometry方法,而是用字符串替换它。改变这个:top.geometry=("100x100")对此:top.geometry("100x100")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python