我如何使用python将终端输出到网格中的tkinter框架?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我如何使用python将终端输出到网格中的tkinter框架?相关的知识,希望对你有一定的参考价值。
我已经为在线上找到的日历样式编写了算法。我希望输入一年后单击按钮在tk窗口中打印算法。不幸的是,它不会。我仅使用此代码作为算法的测试实现来测试算法。该算法将通过输入“ Mer”和年份(例如“ 2020”)来打印到终端,但是该算法不会在与终端相同的网格中打印到tkinter。它根本不会打印到tkinter窗口,并且没有错误消息,所以我不确定下一步该怎么做。
def make_calendar(year, start_day):
tup = [('Early Winter', range(1, 37, + 1)),
('Mid Winter 37,', range(37, 1, + 1)),
('Late Winter ', range(38, 71, + 1)),
('Leap Day ', range(70, 69 + 1)),#tup[3] is this line i need to delete if not leap year
('Late Winter ', range(71, 74, + 1)),
('Early Spring ', range(1, 37, + 1)),
('Mid Spring 37,', range(37, 1, +1)),
('Late Spring ', range(38, 74, + 1)),
('Early Summer ', range(1, 37, + 1)),
('Mid Summer 37,', range(37, 1, +1)),
('Late Summer ', range(38, 74, + 1)),
('Early Autumn ', range(1, 37, + 1)),
('Mid Autumn 37,', range(37, 1, +1)),
('Late Autumn ', range(38, 74, + 1)),
('Early Fall ', range(1, 37, + 1)),
('Mid Fall 37, ', range(37, 1, +1)),
('Late Fall ', range(38, 74, + 1))];
week = ['Mer', 'Ven', 'Ear', 'Mar', 'Jup', 'Sat', 'Ura', 'Nep', 'Plu']
start_pos = week.index(start_day)
def is_leap(year):
if (not year%4 and year%100 or not year%400): # algorithm to determine leap year
return True
if not is_leap(year):
del tup[3]
for month, days in tup:
# Print month title
print('{0} {1}'.format(month, year).center(20, ' '))
# Print Day headings
print(''.join(['{0:<3}'.format(w) for w in week]))
# Add spacing for non-zero starting position
print('{0:<3}'.format('')*start_pos, end='')
for day in days:
# Print day
print('{0!s:<3}'.format(day), end='')
start_pos += 1
if start_pos == 9:
# If start_pos == 9 (Sunday) start new line
print()
start_pos = 0 # Reset counter
print('
')
start_day=(input('Enter start day of the year Mer,Ven,Ear,Mar,Jup,Sat,Ura,Nep,Plu'))
year=int(input('Enter Year'))
make_calendar(year, start_day)
这里,我已经设计了一个tkinter窗口,算法应该将其打印到该窗口,但不会将其打印到tk窗口,并且我不会收到任何错误消息。因此,基本上该算法可以在终端中工作并打印,但是当编码为在tkinter窗口中工作时,它什么也不返回。如果算法无法打印到它,则我创建的窗口小部件将无用。我已经测试了算法,并且可以正常工作。我已经测试了tkinter按钮,它们似乎无法打印。我不确定如何解决此问题。
import tkinter as tk
#varContent = inputentry.get() # get what's written in the inputentry entry widget
#outputtext.delete('0', 'end-1c') # clear the outputtext text widget
#outputtext.insert(varContent) # print content
#these functions may be relevant to the solution I cannot figure out their implementation in this code.
HEIGHT = 700
WIDTH = 800
root = tk.Tk() #APPLICATION IS BETWEEN THIS LINE DOWN
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
frame = tk.Frame(root, bg='#80c1ff', bd='5')
frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n')
button = tk.Button(frame, text="Display Calendar", command=lambda: make_calendar(entry.get()))
button.grid(row=0, column=2)#this makes the buton apear in a grid
label = tk.Label(frame, text="Enter the year")
label.grid(row=0, column=0)#this makes the label apear in a grid
entry = tk.Entry(frame)
entry.grid(row=0, column=1)#this makes the entry apear in a grid
lower_frame = tk.Frame(root, bg='#80c1ff', bd='5')
lower_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.6, anchor='n')
lower_label = tk.Text(lower_frame)
lower_label.place(relwidth=1, relheight=1)#this makes the label apear in a grid
year=entry.get
def make_calendar(year):
tup = [('Early Winter', range(1, 37, + 1)),
('Mid Winter 37,', range(37, 1, + 1)),
('Late Winter ', range(38, 71, + 1)),
('Leap Day ', range(70, 69 + 1)),#tup[3] is this line i need to delete if not leap year
('Late Winter ', range(71, 74, + 1)),
('Early Spring ', range(1, 37, + 1)),
('Mid Spring 37,', range(37, 1, +1)),
('Late Spring ', range(38, 74, + 1)),
('Early Summer ', range(1, 37, + 1)),
('Mid Summer 37,', range(37, 1, +1)),
('Late Summer ', range(38, 74, + 1)),
('Early Autumn ', range(1, 37, + 1)),
('Mid Autumn 37,', range(37, 1, +1)),
('Late Autumn ', range(38, 74, + 1)),
('Early Fall ', range(1, 37, + 1)),
('Mid Fall 37, ', range(37, 1, +1)),
('Late Fall ', range(38, 74, + 1))];
week = ['Mer ', 'Ven ', 'Ear ', 'Mar ', 'Jup ', 'Sat ', 'Ura ', 'Nep ', 'Plu ']
start_pos = week.index('Mer ')
def is_leap(year):
if (not year%4 and year%100 or not year%400): # algorithm to determine leap year
return True
if not is_leap(year):
del tup[3]
for month, days in tup:
# Print month title
print('{0} {1}'.format(month, year).center(20, ' '))
# Print Day headings
print(''.join(['{0:<3}'.format(w) for w in week]))
# Add spacing for non-zero starting position
print('{0:<3}'.format('')*start_pos, end='')
for day in days:
# Print day
print('{0!s:<6}'.format(day), end='')
start_pos += 1
if start_pos == 9:
# If start_pos == 9 (Sunday) start new line
print()
start_pos = 0 # Reset counter
def make_calendar(year):
button2 = tk.Button(frame, text="Print Calendar", function=print('
'))
button2.grid(row=0, column=5)#this makes the buton apear in a grid
start_day=('Mer ')
make_calendar(year)
root.mainloop()#APPLICATION IS BETWEEN THIS LINE^
答案
我在您解决的代码中发现了一些错误。希望这将帮助您前进:
- 方法make_calendar被声明两次。我删除了第二个,因为应该在创建其他tk元素的地方创建button2。
- is_leap从tk输入中获得了一个字符串,但它需要一个整数
- calculate_calendar直接打印到控制台。那很不好。我将其更改为返回字符串。现在我们可以有两个方法print_calendar和display_calendar。两者都使用来自calculate_calendar的结果。
- 不确定tkinter Text对象的工作方式。我将您的lower_label更改为tk.Label,现在我可以更新内容了。
以下代码对我有用-它在TK标签中显示正确的内容,并且可以打印到控制台。
import tkinter as tk
# varContent = inputentry.get() # get what's written in the inputentry entry widget
# outputtext.delete('0', 'end-1c') # clear the outputtext text widget
# outputtext.insert(varContent) # print content
# these functions may be relevant to the solution I cannot figure out their implementation in this code.
from tkinter import END
HEIGHT = 700
WIDTH = 800
root = tk.Tk() # APPLICATION IS BETWEEN THIS LINE DOWN
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()
frame = tk.Frame(root, bg='#80c1ff', bd='5')
frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n')
button = tk.Button(frame, text="Display Calendar", command=lambda: display_calendar(entry.get()))
button.grid(row=0, column=2) # this makes the buton apear in a grid
button2 = tk.Button(frame, text="Print Calendar", command=lambda: print_calendar(entry.get()))
button2.grid(row=0, column=5) # this makes the buton apear in a grid
label = tk.Label(frame, text="Enter the year")
label.grid(row=0, column=0) # this makes the label apear in a grid
entry = tk.Entry(frame)
entry.grid(row=0, column=1) # this makes the entry apear in a grid
lower_frame = tk.Frame(root, bg='#80c1ff', bd='5')
lower_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.6, anchor='n')
lower_label = tk.Label(lower_frame)
lower_label.place(relwidth=1, relheight=1) # this makes the label apear in a grid
year = entry.get
def print_calendar(year):
calendar = calculate_calendar(year)
print(calendar)
def display_calendar(year):
calendar = calculate_calendar(year)
lower_label.config(text=calendar)
def calculate_calendar(year):
result = ""
tup = [('Early Winter', range(1, 37, + 1)),
('Mid Winter 37,', range(37, 1, + 1)),
('Late Winter ', range(38, 71, + 1)),
('Leap Day ', range(70, 69 + 1)), # tup[3] is this line i need to delete if not leap year
('Late Winter ', range(71, 74, + 1)),
('Early Spring ', range(1, 37, + 1)),
('Mid Spring 37,', range(37, 1, +1)),
('Late Spring ', range(38, 74, + 1)),
('Early Summer ', range(1, 37, + 1)),
('Mid Summer 37,', range(37, 1, +1)),
('Late Summer ', range(38, 74, + 1)),
('Early Autumn ', range(1, 37, + 1)),
('Mid Autumn 37,', range(37, 1, +1)),
('Late Autumn ', range(38, 74, + 1)),
('Early Fall ', range(1, 37, + 1)),
('Mid Fall 37, ', range(37, 1, +1)),
('Late Fall ', range(38, 74, + 1))]
week = ['Mer ', 'Ven ', 'Ear ', 'Mar ', 'Jup ', 'Sat ', 'Ura ', 'Nep ', 'Plu ']
start_pos = week.index('Mer ')
def is_leap(year):
year = int(year)
if not year % 4 and year % 100 or not year % 400: # algorithm to determine leap year
return True
if not is_leap(year):
del tup[3]
for month, days in tup:
# Print month title
result += '{0} {1}
'.format(month, year).center(20, ' ')
# Print Day headings
result += ''.join(['{0:<3}'.format(w) for w in week]) + "
"
# Add spacing for non-zero starting position
result += '{0:<3}'.format('') * start_pos
for day in days:
# Print day
result += '{0!s:<6}'.format(day)
start_pos += 1
if start_pos == 9:
# If start_pos == 9 (Sunday) start new line
result += "
"
start_pos = 0 # Reset counter
return result
start_day = ('Mer ')
root.mainloop() # APPLICATION IS BETWEEN THIS LINE^
您将要在其下添加一个滚动条,因为虽然您看不到整个文本:)
以上是关于我如何使用python将终端输出到网格中的tkinter框架?的主要内容,如果未能解决你的问题,请参考以下文章