[英]Trouble with the Tkinter Grid manager when a canvas is placed in window
Python, tkinter, I'm trying to write a program that takes a file and uses its contents to drawl a picture. Python、tkinter,我正在尝试编写一个程序,它获取一个文件并使用其内容来绘制图片。 I can't get the grid manager to line the widgets up correctly.
我无法让网格管理器正确排列小部件。
I want it to look like the first picture, but it always comes out looking like the second: https://imgur.com/a/hS7gyqq我希望它看起来像第一张图片,但它总是看起来像第二张: https://imgur.com/a/hS7gyqq
In my code, the, "Enter a file name" is in column 0, which should make it all the way to the left.在我的代码中,“输入文件名”位于第 0 列,应该一直到左边。 But it only shows up about half way through the window.
但它只出现在 window 的一半左右。
Here is the code:这是代码:
from tkinter import *
from tkinter import filedialog
from tkinter.filedialog import askopenfilename
class LetterCounter():
def __init__(self):
#create the window
window=Tk()
window.title("Occurance Of Letters")
window.columnconfigure((1,2,3,4,5,6,7,8,9,10), weight=1)
window.rowconfigure((1,2,3,4,5,6,7), weight =1)
window.geometry("700x400")
#create the canvas
self.canvas = Canvas(window, width=500, height=300, bg="blue")
self.canvas.grid(column=0, row=0, padx=100, pady=20)
BtshowResults = Button(window, text ="Show Results", command = self.showResults).grid(column=6, row = 7)
BtnBrowse = Button(window, text="Browse", command =self.browseFile).grid(column=4, row =7)
#create the buttons
self.EnterdFile = StringVar()
Entry(window, textvariable = self.EnterdFile).grid(column=1, row=7)
Label(window, text="Enter a filename").grid(column=0, row=7)
window.mainloop()
#browse file function
def browseFile(self):
filenameForReading = askopenfilename()
self.EnterdFile.set(filenameForReading)
#show result function
def showResults(self):
filenameForReading = self.EnterdFile.get()
letterCount = {} #create empty dictionary
infile = open(filenameForReading, "r")
for line in infile:
self.processLine(line.lower(), letterCount)
y = 10
for ch, count in sorted(letterCount.items()):
x=500
text = f'letter { ch} appears { count}times.'
self.canvas.create_text(x, y, text=text, fill="black", font=('Helvetica 15 bold'))
y+=20
def processLine(self, line, letterCount):
line = self.replacePunction(line)
for ch in line:
if ch in letterCount:
letterCount[ch] +=1
else:
letterCount[ch] = 1
#replaces punction with blank space
def replacePunction(self, line):
for ch in line:
if not ch.isalpha():
line = line.replace(ch, "")
return line
LetterCounter()
So I have tried adjusting the columns and rows.所以我尝试调整列和行。
I know the problem is tied to the canvas widget, because when I remove it, the lower widgets behave the way I want them to:我知道问题与 canvas 小部件有关,因为当我删除它时,较低的小部件会按照我希望的方式运行:
https://imgur.com/a/eqcc5rP https://imgur.com/a/eqcc5rP
How do I make the lower widgets behave in the way I want them to?如何使较低的小部件按照我希望的方式运行? AKA neatly spaced at the bottom like a normal GUI window?
又名像普通 GUI window 一样在底部整齐排列?
PS: this is only my 3d stack overflow question, I tried searching the problem, but I apologize in advance if I overlooked the existing answer. PS:这只是我的 3d 堆栈溢出问题,我尝试搜索问题,但如果我忽略了现有答案,请提前致歉。
You can set the columnspan
option on self.canvas.grid(...)
to make the canvas occupies several columns:您可以在
self.canvas.grid(...)
上设置columnspan
选项,使 canvas 占据多个列:
# you can adjust the value of columnspan to suit your case
self.canvas.grid(column=0, row=0, padx=100, pady=20, columnspan=8)
Result:结果:
But for flexibility, I would suggest to put the bottom widgets inside a frame.但为了灵活性,我建议将底部小部件放在框架内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.