gpt4 book ai didi

python - Tkinter 使用 {} 显示文本

转载 作者:可可西里 更新时间:2023-11-01 10:20:03 24 4
gpt4 key购买 nike

基本系统信息:Windows 7 64位,python 3.4.3 64位,运行PyCharm教育版1.0.1

我编写了一个程序,向用户显示一个带有下拉菜单和按钮的窗口(示例接近结尾)。下拉菜单有一个自行车 ID 号列表,用户可以从中选择,然后单击按钮,将显示文本,告诉用户他们选择了什么自行车以及骑了多少次。这是代码:

1    from tkinter import *
2 import pandas as pd
3 from tkinter import ttk
4 from collections import OrderedDict
5
6 # read in the sorted bike file
7 data = pd.read_csv('Sorted_Bike_Uses.csv', header=0)
8
9 bike = data['Bike ID'] # put the bike id's into a series
10 bike = bike.tolist() # make the series a list
11 uses = data['Number of Uses'] # put the number of uses into a series
12 uses = uses.tolist() # make the series a list
13 a = OrderedDict(list(zip(bike, uses))) # put the bike ids ans uses into a dictionary, preserving their order
14 bike.sort() # sort the bike id's for the drop down list
15
16
17 # this function updates the text displayed on the box
18 def select():
19 sf = "Bicycle #%s" % var.get()
20 root.title(sf)
21 x = int("%s" % var.get())
22 display.configure(text=("Bicycle", x, "was used", a[x], "times."))
23 display.pack()
24
25 # sets up the window
26 root = Tk()
27 root.geometry("%dx%d+%d+%d" % (330, 80, 200, 150))
28 root.title("Optionmenu as combobox")
29
30 # sets up how it will display the information
31 display = Label(root, text="")
32
33 # starts the drop down box at 14529
34 var = StringVar(root)
35 var.set('14529')
36
37 # sets up the drop down list
38 w = ttk.Combobox(root, textvariable=var, values=bike)
39 w.pack()
40
41 # sets up the button used to select a bike id
42 button = Button(root, text='Analyze!', command=select)
43 button.pack(side='left', padx=20, pady=10)
44
45 root.mainloop()

The problem is that when a bike id is chosen the output text has {} added in around "was used".例如:

enter image description here

我不知道它们为什么在那里或如何删除它们。这并不是一个大问题,但我想要更统一的格式。

还有第二个问题是,当我去选择不同的自行车 ID 时,选项的数字中包含逗号。因此,如果我先选择 14529,如上例所示,然后选择 14530,则 14530 将看起来像“14530”。这会导致程序无法运行,除非我在按下按钮之前删除逗号。同样,只要我删除逗号,该程序就会运行,但我想弄清楚如何解决这个问题。

最佳答案

花括号中有一点点 Tcl 泄漏。您已经传递了一个 Python 列表来设置此行中的显示:

display.configure(text=("Bicycle", x, "was used", a[x], "times."))

当应用于 Tk 小部件时,这将被转换为 Tcl 列表,并且在 Tcl 中,一个带有空格的列表元素将以大括号引号显示。

正如@jonrsharpe 所指出的,您应该将标签文本设置为字符串以避免这种情况。

关于python - Tkinter 使用 {} 显示文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31186125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com