简介:
大家好,我没有编程方面的经验,并且毫不奇怪我的第一个程序遇到了一些困难...如果有人能帮助我克服这条路上的驼峰,那将是有责任的。
我的问题背景:
我在从Tkinter输入框调用整数时遇到问题。设置如下,我有一个带描述符标签的框架,一个输入框,一个确认按钮和一个确认标签。我想到的设计是让用户输入一个数字,点击确认按钮,这会导致存储输入并更新确认标签。这部分我没有任何问题,并且按照希望工作......
from tkinter import *
root = Tk()
root.geometry('450x700+10+10')
root.title("Design Suite Version: 1.0")
#Creating a labelframe for my entry box/button and confirmation label
myframe2 = LabelFrame(root, text = '2: Limit state design selections:')
myframe2.grid(row=0, column=1, padx=5, pady=5, sticky = 'nw')
#Label identifying input I expect in my entry box...
Label(myframe2,
text="Proposed Member Depth:",
justify = LEFT).grid(row=21, column=0, padx=5, pady=5)
#Constructing the function for the self updating label
def Selection6():
#text expected in the confirmation label
labeltext6 = "Depth(mm) = " + str(var6.get())
#configuring the updating label
label6.config(text = labeltext6, fg='red', padx=5, pady=5)
#confirmation label location
label6 = Label(myframe2)
#confirmation label location
label6.grid(row=27, column=0, padx=0, pady=5)
#set what var is here
var6 = IntVar()
# initializing the choice
var6.set('0')
#this is my first entry box
entry6 = Entry(myframe2, textvariable=var6, width = 25)
entry6.grid(row=25, column=0, padx=5, pady=5)
#this is my first button, when clicked, runs Selection6
button = Button(myframe2, text="Click to Confirm",width = 20,
command = Selection6)
button.grid(row=26, column=0, padx=5, pady=0)
##########################################################################
#Creating a 2nd label for my entry box/button and confirmation label
Label(myframe2,
text="Proposed Member Width:",
justify = LEFT).grid(row=28, column=0,padx=5, pady=5)
def Selection6a():
labeltext6a = "Width(mm) = " + str(var6a.get())
label6a.config(text = labeltext6a, fg='red', padx=5, pady=5)
label6a = Label(myframe2)
label6a.grid(row=31, column=0, padx=0, pady=5)
var6a = IntVar()
var6a.set('0')
entry6a = Entry(myframe2, textvariable=var6a, width = 25)
entry6a.grid(row=29, column=0, padx=5, pady=5, sticky = 'w')
button = Button(myframe2, text="Click to Confirm",width = 20,
command = Selection6a)
button.grid(row=30, column=0, padx=5, pady=0)
mainloop()
初始问题:
我最初遇到困难的地方是当我尝试从我的两个输入框(上面)获取变量输入并通过添加以下代码在我的程序(下面)中使用我的GUI进一步使用它们时... / p>
##########################################################################
#Creating a 3rd label for my button and confirmation label
Label(myframe2,
text="Member Modulus:",
justify = LEFT).grid(row=32, column=0,padx=5, pady=5)
def Selection7():
labeltext7 = "Modulus(mm cubed) = ",
+ (((str(var6a.get()) * (str(var6.get()))**2))/6)
label7.config(text = labeltext7, fg='red', padx=5, pady=5)
label7 = Label(myframe2)
label7.grid(row=34, column=0, padx=0, pady=5)
var7 = IntVar()
var7.set('0')
button = Button(myframe2, text="Click for Modulus",width = 20,
command = Selection7)
button.grid(row=33, column=0, padx=5, pady=0)
mainloop()
当我尝试在函数中使用var6.get()和var6a.get()来更新标签时,我遇到如下错误:
labeltext7 = "Modulus(mm cubed) = " + (((str(var6a.get()) * (str(var6.get()))**2))/6)TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int' Exception in Tkinter callback
尝试修复:
我试图通过重写我的函数将字符串转换为整数,如下所示....
def Selection7():
labeltext7 = "Modulus(mm cubed) = ",
+ (((int(var6a.get()) * (int(var6.get()))**2))/6)
label7.config(text = labeltext7, fg='red', padx=5, pady=5)
当前问题:
当我通过Selection7()
致电button = Button(myframe2, text="Click for Modulus"
时,我没有将计算出的值输出到labeltext7
。脚本现在执行正常,没有错误,这是我的问题,我无法得到计算输出函数...欢迎提出任何意见,建议或建议!
该网站建议的一些帖子我已阅读但无法找到答案:
How to get values from entry box with tkinter
tkinter set values in entry box
https://stackoverflow.com/questions/26935846/tkinter-entry-box-issues
Python posting answer from function to entry box in GUI
http://effbot.org/tkinterbook/entry.htm
UPDATE1:
嗨再次,感谢您抽出时间发表评论并回答。我包含了一个docstring来解释我希望用我的函数实现的内容,希望这会进一步澄清请求。
我将紧凑的代码分成了小块,并根据建议删除了逗号。
这是修改后的代码部分,我仍然无法输出所需的功能,感谢任何帮助。
Label(myframe2,
text="Member Modulus:",
justify = LEFT).grid(row=32, column=0,padx=5, pady=5)
"""
Selection 7()_Member Modulus:
Modulus for a rectangular section:
(Width of member x depth of member squared) divided by 6
width:var6a depth:var6
"""
def Selection7():
wdth = int(var6a.get())
dpth = int(var6.get())
dpthsqrd = int(dpth**2)
wdth_dpthsqrd = int((wdth * dpthsqrd))
mod = int(wdth_dpthsqrd/6)
labeltext7 = "Modulus (mm cubed) = "
+ int(mod)
label7.config(text = labeltext7, fg='red', padx=5, pady=5)
label7 = Label(myframe2)
label7.grid(row=34, column=0, padx=0, pady=5)
var7 = IntVar()
var7.set('0')
button = Button(myframe2, text="Click for Modulus",width = 20,
command = Selection7)
button.grid(row=33, column=0, padx=5, pady=0)
mainloop()
UPDATE1.1:
大家好,在更新1中,我应该扩展我的问题。问题是defSelection7()
中的计算没有像labeltext7
那样显示在GUI上,因为Modulus (mm cubed) =
是Selection7
时文本int(mod)
显示在GUI上已激活,但计算defSelection7()
不是。
UPDATE2:
更幸福的是,我的问题已经解决,我已设法让int(mod)
显示labeltext7
int(mod)
部分,
移动"""
Selection 7()_Member Modulus:
Modulus for a rectangular section:
(Width of member x depth of member squared) divided by 6
width:var6a depth:var6
"""
def Selection7():
wdth = int(var6a.get())
dpth = int(var6.get())
dpthsqrd = (dpth**2)
wdth_dpthsqrd = (wdth * dpthsqrd)
mod = (wdth_dpthsqrd/6)
labeltext7 = "Modulus (mm cubed) = ", mod
label7.config(text = labeltext7, fg='red', padx=5, pady=5)
label7 = Label(myframe2)
label7.grid(row=34, column=0, padx=0, pady=5)
var7 = IntVar()
var7.set('0')
button = Button(myframe2, text="Click for Modulus",width = 20,
command = Selection7)
button.grid(row=33, column=0, padx=5, pady=0)
mainloop()
一行包括labeltext7 = "Modulus (mm cubed) = "
+ int(mod)
。
请参阅下面的修订代码。
labeltext7 = "Modulus (mm cubed) = ", mod
我很高兴我的GUI功能如预期的那样。如果有人可以发布一个链接供我阅读,这将有助于我确定我的修订工作的原因,我会更高兴{压缩int(mod)
来自UPDATE1
到此UPDATE2修订版<div id="score">50</div>
然后我的GUI按预期更新,正确地将计算div
拉到并显示到&#39; labeltext7&#39;。
答案 0 :(得分:0)
删除逗号应该有效:
def Selection7():
labeltext7 = "Modulus(mm cubed) = "
+ (((int(var6a.get()) * (int(var6.get()))**2))/6)
label7.config(text = labeltext7, fg='red', padx=5, pady=5)
还是更好:
def Selection7():
width = int(var6a.get())
labeltext7 = "Modulus(mm cubed) = " + (width**3/6)
label7.config(text = labeltext7, fg='red', padx=5, pady=5)