【发布时间】:2022-11-03 22:44:10
【问题描述】:
我正在创建一个显示美国城市房地产图表的程序。我能够创建一个主要功能,主要涉及创建每个城市的图表。
现在,我正在尝试添加一个新功能,它允许用户通过创建一个组合框在众多选项中选择一个城市。
我要做的基本上只是允许用户在组合框中的多个城市中单击一个选项,当用户单击它时,它应该自动调用主函数,以便主函数可以生成选定的图形。
我正在为我的 GUI 设计使用 tkinter 和 Custom Tkinter 模块。
代码:
#Libraries
(...) # This is for graphing features
#Tkinter
from tkinter import *
import tkinter as tk
from PIL import ImageTk, Image
import customtkinter as ctk
import requests
import tkinter.messagebox
ctk.set_appearance_mode("Light")
ctk.set_default_color_theme("dark-blue")
class App(ctk.CTk,tk.Tk):
WIDTH = 780
HEIGHT = 520
def __init__(self):
super().__init__()
self.geometry(f"{700}x{500}")
self.title("Title of My Program")
self.protocol("Window Closed", self.stop) # "stop" function called when program closed
self.resizable(0,0)
# Options for light & Dark mode
self.option1 = ctk.CTkOptionMenu(master=self,
values=["Light", "Dark", "System"],
width=30,
height=30,
command=self.windowcolor)
self.option1.place(x=5, y=10)
self.option1.set("System") # Select default color for buttons
# Create center label
self.label1 = ctk.CTkLabel(master=self,
text="Graph is generated if you click one option from the below combobox.")
self.label1.place(x=200,y=10)
# City list
cities = ["LA", "CA", "IN", "AK" # etc ...]
# Center Combobox
global combobox1
self.cb_var = StringVar()
self.combobox1 = ctk.CTkComboBox(
master=self,
values=cities,
command=main,
variable=self.cb_var,
)
self.combobox1.place(x=280, y=50)
# Create center frame
self.frameCenter = ctk.CTkFrame(master=self,
width=682,
height=370,
corner_radius=5)
self.frameCenter.place(x=9, y=120)
global main
def main(self): # Main function
self.tkinter.messagebox.showinfo(title="Combobox", message="Clicked")
if combobox1.command == "CA":
graph_CA(self)
# graph photo is generated here
self.data = pd.read_excel("MyExcelFile.xlsx", sheet_name="MySheetName")
# Remove ctkCombobox, create a window fits for graph photo
def graph_CA(self):
# Graphing features added here
# Function that changes color of window
def windowcolor(self, new_appearance_mode):
ctk.set_appearance_mode(new_appearance_mode)
# Function that stops program
def stop(self, event=0):
self.destroy()
if __name__ == "__main__":
app = App()
app.mainloop()
问题:当我运行这段代码时,一切正常,除了它会产生这个错误:
“str”对象没有属性“tkinter”
在我单击中心组合框中的任何选项后。
主函数工作正常并且生成图形很好,但程序甚至在到达主函数之前就停止了。
问题:当用户单击其中的任何选项时,如何制作一个可以调用主函数的组合框?
【问题讨论】:
标签: python python-3.x tkinter