python - How to get pass error : TypeError: addtrans() missing 1 required positional argument: 'self'
问题描述
i created a button function 'addtrans" that will create another pop up window and collect some info from user entered into a entry box. That function is a button command. When i click button nothing comes up except the error.
def addtrans(self):
newWin = Toplevel(root)
width = 600
height = 400
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height,
(screenwidth - width) / 2, (screenheight - height) / 2)
newWin.geometry(alignstr)
newWin.resizable(width=False, height=False)
self.lbl1 = Label(newWin, text="Enter amount in Euros :", fg='red')
self.lbl1.place(x=150 , y=50)
self.euros = Entry(newWin, width=20)
self.euros.place(x=300, y=50)
self.lbl2 = Label(newWin, text="CATEGORY :", fg='green')
self.lbl2.place(x=150 , y=80)
self.cat_entry = Entry(newWin, width=20)
self.cat_entry.place(x=299, y=80)
self.place_entry = Label(newWin, text=" ENTER NAME OF PLACE :")
self.place_entry.place(x=140, y=120)
self.txtfld2 = Entry(newWin, width=20)
self.txtfld2.place(x=299, y=120)
self.submit_btn=Button(newWin, text="SUBMIT RECORD TO DATABASE", command=submit)
self.submit_btn.place(x=280, y=160)
解决方案
假设您的代码如下:
class Example(object):
def __init__(self, root, command):
self.root = root
self.command = command
def addtrans(self):
root = self.root
command = self.command
newWin = Toplevel(root)
width = 600
height = 400
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height,
(screenwidth - width) / 2, (screenheight - height) / 2)
newWin.geometry(alignstr)
newWin.resizable(width=False, height=False)
self.lbl1 = Label(newWin, text="Enter amount in Euros :", fg='red')
self.lbl1.place(x=150 , y=50)
self.euros = Entry(newWin, width=20)
self.euros.place(x=300, y=50)
self.lbl2 = Label(newWin, text="CATEGORY :", fg='green')
self.lbl2.place(x=150 , y=80)
self.cat_entry = Entry(newWin, width=20)
self.cat_entry.place(x=299, y=80)
self.place_entry = Label(newWin, text=" ENTER NAME OF PLACE :")
self.place_entry.place(x=140, y=120)
self.txtfld2 = Entry(newWin, width=20)
self.txtfld2.place(x=299, y=120)
self.submit_btn=Button(newWin, text="SUBMIT RECORD TO DATABASE", command=command)
self.submit_btn.place(x=280, y=160)
您可以像这样实例化和调用:
import tkinter
win = tkinter.Tk()
example = Example(win, lambda: print("submit!"))
example.addtrans()
win.mainloop()
如果您单击“提交记录到数据库”按钮,它将打印“提交!”
推荐阅读
- google-apps-script - 到达包含当前用户电子邮件 ID 的行
- vue.js - 使用 vue 过渡实现像手风琴一样的幻灯片
- android - Android - 是否有与 Xamarin 表单 DataTemplate 等效的 Android?
- javascript - Momentjs中日期格式的问题
- c# - 如何在 discord.net 中获取指定的不和谐文本频道 ID
- node.js - Apollo federation 抛出错误:标记为 @external 但未在基础服务上定义
- python - 基于多个条件语句熊猫数据框创建新列
- sql-server - 在 SQL Server 中使用包含的全文索引搜索问题
- c - 为什么 for 循环不将元素放入数组中?
- wordpress - WooCommerce 中的“woocommerce_review_order_after_payment”挂钩问题