Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我的tkinter toplevel在我声明尺寸限制时很大?_Python_Tkinter_Toplevel - Fatal编程技术网

Python 为什么我的tkinter toplevel在我声明尺寸限制时很大?

Python 为什么我的tkinter toplevel在我声明尺寸限制时很大?,python,tkinter,toplevel,Python,Tkinter,Toplevel,我知道这可能是个新问题,但我还没有找到答案。下面是我的一段代码,它有一个根窗口,其中包含一个打开顶层的按钮。顶层从文本文件中随机抽取一行,作为一种想法生成器 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 directo

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

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()

问题是,Toplevel总是显示得非常巨大(比我的根窗口还大),因为里面显示的内容很少,所以看起来非常愚蠢。我是不是错过了一些很小很愚蠢的事情?非常感谢您的帮助。

问题在于您没有调用
几何体
方法,而是将其替换为字符串

更改此项:

top.geometry=("100x100")
为此:

top.geometry("100x100")

这段代码有很多问题阻止它运行。请将您的问题包括在工作表中。如果您的示例使用一些硬编码数据而不是依赖外部文件,这也会有所帮助。@BryanOakley已经完成了,谢谢您指出这一点。外部文件实际上只是一个纯文本列表,每行有新条目,每个条目大约10个单词长。似乎有几行代码与问题无关。为什么我们需要这些按钮?一个按钮不行,还是只有当你有三个按钮时问题才会出现?此外,您似乎正在以至少两种不同的方式导入tkinter。请告诉我们您是如何导入tkinter的。@BryanOakley再次修复。对不起,你可以告诉我python不是我的强项。摆脱
top.geometry=(“100x100”)
?并将padx和pady添加到文本框中