我刚刚开始学习Python,我正在尝试创建一个矿物计数器,只使用用于显微岩石学的字母,但我在将python代码传递给Tkinker时遇到了问题。你们能给出一些如何让我的输出工作的小贴士吗?我发现使用get()方法很有挑战性,即使是在线教程也是如此。
你们能教这个菜鸟吗?谢谢!
我的原始代码:
# define sample
sample = "qreqqwer"
# mineral q:
mineralq= "q"
countq = sample.count(mineralq)
# print count of q
print("The quantity of q is: ", countq)我用Tkinker做的结构:
from tkinter import *
import tkinter as tk
# Window
window=tk.Tk()
window.title("Mineral Counter")
window.geometry("800x1000")
window.configure(bg="#00090F")
inputUser=tk.Text(window,width=225,height=5,font=("Arial bold",12),wrap="word", bg="#00090F", fg="white")
inputUser.pack()
# define sample
# mineral q:
countq = inputUser.count("q")
# print count of q
output.insert(tk.INSERT,countq)
output=tk.Text(window,width=20,height=2,font=("Arial bold",12), bg="#00090F", fg="white")
output.pack()
window.mainloop()发布于 2020-08-25 04:52:31
您需要一个按钮来更新您的代码,因为最初Text框是空的,因此不会出现q,因此不能插入任何内容。
试试这个:
首先创建一个按钮,该按钮具有在输入数据后单击的功能
b = tk.Button(window, text='Click Me', command=click)
b.pack()然后定义按钮在单击时调用的函数
def click():
sample = inputUser.get('1.0', 'end-1c') #getting value from text box 1
mineralq = 'q' #letter to be counted
countq = sample.count(mineralq) #counting the letter
output.insert('1.0', f'The quantity of q is: {countq}') #inserting the output to text box 2希望它消除了你的疑虑,如果有任何错误,请让我知道
干杯
https://stackoverflow.com/questions/63568437
复制相似问题