我需要理解模型/视图/控制器方法背后的概念以及如何以这种方式编写GUI.这里只是一个非常基本的简单GUI.有人可以向我解释如何使用MVC重写此代码吗?
from tkinter import *
class Application(Frame):
""" GUI application that creates a story based on user input. """
def __init__(self, master):
""" Initialize Frame. """
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
""" Create widgets to get story information and to display story. """
# create instruction label
Label(self,
text = "Enter information for a new story"
).grid(row = 0, column = 0, columnspan = 2, sticky = W)
# create a label and text entry for the name of …Run Code Online (Sandbox Code Playgroud) 我需要编写一个函数,它接受一个字符和一个字符串作为输入,然后将该字符与字符串中的每个元素进行比较.然后打印并最终返回字符出现在字符串中的次数.
这是我提出的代码,但它没有以正确的方式运行.如果有人能解释并纠正错误,我会很感激.
我想先写一个比较两个字符的函数来检查它们是否相等,如下所示:
def func1(x1, x2):
if x1 == x2:
return True
else:
return False
Run Code Online (Sandbox Code Playgroud)
然后,我想我会选择另一个主要功能:
def func2():
ch1 = input("Enter one character. ")
str1 = str(input("Enter a string. "))
list_1 = list(str1)
a = 0
for 1 in list_1:
if func1(ch1, list_1):
a += 1
else:
a += 0
print(a)
return a
Run Code Online (Sandbox Code Playgroud)
这里有什么错误?如果我选择"a"作为我的角色,然后输入一个五个字母的字符串作为我的字符串,该函数仍然告诉我"a"只出现在字符串中一次.为什么这样,我该如何解决?
如果我有这样的二维列表:
TopRow = [1, 3, 5]
MiddleRow = [7, 9, 11]
BottomRow = [13, 15, 17]
matrix = [TopRow, MiddleRow, BottomRow]
Run Code Online (Sandbox Code Playgroud)
我需要创建一个函数,它接受2维列表和两个值,row和col,作为输入,然后在指定的行和二维列表的列中打印出指定的数字.假设row和col的定义如下:
row = 2
col = 3
Run Code Online (Sandbox Code Playgroud)
为什么这段代码没有检索到值(在本例中为11)并将其打印出来?
def get_value(matrix, row, col):
print(matrix[row][col])
Run Code Online (Sandbox Code Playgroud)