我知道這可能是一個新問題,但我還沒有找到答案。這是我的代碼片段,其中有一個根窗口,其中包含一個用于打開頂級的按鈕。頂層從文本文件中隨機抽取一行,作為一種想法生成器。import random, fileinputimport tkinter as tkfrom tkinter import *root = tk.Tk()root.title('Daydreamer')#fname should be the file name of the image in working directoryfname = "bg.gif"bg_image = tk.PhotoImage(file=fname)#get width and height of imagew = 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()問題是,頂級總是絕對巨大(比我的根窗口大),對于其中顯示的少量內容來說,這看起來非常愚蠢。我在這里錯過了一些非常微小和愚蠢的事情嗎?非常感謝幫助。
1 回答

慕森卡
TA貢獻1806條經驗 獲得超8個贊
問題是您沒有調用該geometry
方法,而是用字符串替換它。
改變這個:
top.geometry=("100x100")
對此:
top.geometry("100x100")
添加回答
舉報
0/150
提交
取消