我想填充一个页面列表,如果从tk界面中单击可以访问这些页面。每个链接旁边都有一个计数器,可以使用+1和-1按钮进行更新;全都在一条线上。所以它应该看起来像: linkl1计数器+ 1button -1button
linkl2计数器+1按钮-1按钮
linkl3计数器+ 1button -1button等...
我有2个我无法解决的问题:1-单击链接在浏览器中无法打开2-单击+1或-1按钮不会更新链接计数器。 有人可以帮我吗?这是代码:
<bbb name="b3"/>
答案 0 :(得分:1)
要使计数器起作用,您需要使用functools.partial
来使“闭包”用作命令。
from functools import partial
#...
self.Plus1Button = tk.Button(self.bottomframe, text = "+1", command=partial(self.plus1, url), bg="green")
要使url打开,您只需要阅读错误消息即可。您在回调函数中缺少参数。试试:
def callback(self, event):
webbrowser.open_new(event.widget.cget("text"))
答案 1 :(得分:0)
您有一组适合逻辑组(一行)的数据和函数……这应该让您大吃一惊,因为它应该是自己的类。这样会更整洁:
import random
import tkinter as tk
import webbrowser
class Pierre:
"""This class holds all the objects, data and functions for a single line"""
def __init__(self, master, url):
self.url = url
self.counter = 0
_, i = master.grid_size() # get the current row number
lbl = tk.Label(master, text=url, fg="blue", cursor="hand2")
lbl.grid(row=i, column=1)
lbl.bind("<Button-1>", self.callback)
self.DisplayButton = tk.Button(master, text = self.counter)
self.DisplayButton.grid(row=i, column=2)
self.DisplayButton.config(height = 1, width = 1 )
self.Plus1Button = tk.Button(master, text = "+1", command=self.plus1, bg="green")
self.Plus1Button.grid(row=i, column=3)
self.Plus1Button.config(height = 1, width = 1 )
self.Neg1Button = tk.Button(master, text = "-1", command=self.neg1, bg="green")
self.Neg1Button.grid(row=i, column=4)
self.Neg1Button.config(height = 1, width = 1 )
def plus1(self):
self.counter += 1
self.DisplayButton["text"]=str(self.counter)
def neg1(self):
self.counter -= 1
self.DisplayButton["text"]=str(self.counter)
def callback(self, event):
webbrowser.open_new(self.url)
class TestClass(tk.Tk):
def __init__(self, **kwargs):
tk.Tk.__init__(self, **kwargs)
self.title('Test')
self.topframe = tk.Frame(self)
self.topframe.pack( side = tk.TOP, pady=30)
self.bottomframe = tk.Frame(self)
self.bottomframe.pack( side = tk.BOTTOM )
self.button = tk.Button(self.topframe, text='Click', command = self.output_value)
self.button.pack(side="left", fill="both", expand=True)
####define the function that the submit button will do
def output_value(self):
urls = ["http://www.google.com", "http://www.facebook.com"]
for url in urls:
Pierre(self.bottomframe, url)
if __name__ == "__main__":
root = TestClass()
root.mainloop()
此外,您不应该这样定义类变量(类中的变量,而不是方法中的变量)。实际上,在您了解非常有用的罕见情况之前,请完全远离类变量。
答案 2 :(得分:0)
@Novel方法的一种变体是使用数据绑定,并且如果有界变量由于某种原因而改变,则让按钮自动更新其文本。
class Pierre:
"""This class holds all the objects, data and functions for a single line"""
def __init__(self, master, url):
self.url = url
self.counter = tk.IntVar(value=0)
_, i = master.grid_size() # get the current row number
lbl = tk.Label(master, text=url, fg="blue", cursor="hand2")
lbl.grid(row=i, column=1)
lbl.bind("<Button-1>", self.callback)
self.DisplayButton = tk.Button(master, textvariable = self.counter)
self.DisplayButton.grid(row=i, column=2)
self.DisplayButton.config(height = 1, width = 1 )
self.Plus1Button = tk.Button(master, text = "+1", command=self.plus1, bg="green")
self.Plus1Button.grid(row=i, column=3)
self.Plus1Button.config(height = 1, width = 1 )
self.Neg1Button = tk.Button(master, text = "-1", command=self.neg1, bg="green")
self.Neg1Button.grid(row=i, column=4)
self.Neg1Button.config(height = 1, width = 1 )
def plus1(self):
self.counter.set(self.counter.get() + 1)
def neg1(self):
self.counter.set(self.counter.get() - 1)
def callback(self, event):
webbrowser.open_new(self.url)