Python tkinter使用grid()发出问题,因为pack()已被使用,而未使用pack()

问题描述 投票:-1回答:1

我正在尝试编写一个使用输入的值更新HTML文件的脚本,该脚本分为2个python文件,每个文件更新一个特定的元素,但是随后在第三个脚本中调用了这两个文件。单独运行每个脚本都可以正常工作,并且没有任何错误,但是从第三个脚本运行两个脚本时,我总是会收到错误消息

File "/Users/bechara/Desktop/coronaReady/python_program/update_stat_page.py", line 8, in <module>
    number=NewNumbers()
  File "/Users/bechara/Desktop/coronaReady/python_program/updating/update_numbers.py", line 9, in __init__
    self.title_label.grid(row=0, column=0, columnspan=2)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 2484, in grid_configure
    self.tk.call(
_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by pack

我已经搜索了两个脚本,没有人使用过pack()方法,我只是使用了grid()。这是第一个代码,单独使用时效果很好:

from pandas import read_csv, melt
import matplotlib
matplotlib.use("TkAgg")
from matplotlib import pyplot as plt

class Graph():
    def __init__(self, country):
        self.in_file=read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv")
        self.in_file_1=self.in_file
        self.in_file_2=self.in_file
        self.in_file_3=self.in_file
        self.leb_file=self.in_file_1.loc[self.in_file_1["Country/Region"]==country,:]
        self.leb_file=self.leb_file.drop(columns=["Lat", "Long", "Province/State"])
        self.leb_file=melt(self.leb_file, id_vars=["Country/Region"])
        self.leb_file=self.leb_file.value.values
        self.file=open(f"/Users/bechara/Desktop/coronaReady/python_program/data/data_corona_{country}.csv", "w")
        self.file.write(str(self.leb_file))
        self.file.close()
        self.file=open(f"/Users/bechara/Desktop/coronaReady/python_program/data/data_corona_{country}.csv", "r")
        self.data=[]
        self.data=self.file.read()
        self.data=self.data.split()
        self.data_len=len(self.data)
        for self.a in range(self.data_len):
            try:
                self.data[a]=int(self.data[self.a])
            except:
                if self.data[self.a]=="[":
                    self.data[self.a]=0
                elif self.data[self.a].endswith("]")==True:
                    self.data[self.a]=int(self.data[self.a][:-1])
        plt.plot(self.data, color="#ff0000")
        plt.xlabel("days since 22/01/2020")
        plt.ylabel("Total cases")
        plt.savefig(f"/Users/bechara/Desktop/coronaReady/python_program/images/data_corona_plot_{country}.png", bbox_inches="tight")
        plt.clf()

这是第二个代码,单独使用也可以正常工作:

from tkinter import Label, Tk, Entry, Button
from webbrowser import open as webopen

class NewNumbers(Tk):
    def __init__(self):
        super(NewNumbers, self).__init__()
        webopen("https://www.worldometers.info/coronavirus/")
        self.title_label=Label(text="Enter The Numbers To Update The\nStatistics Page", font=("Arial, Helvetica, sans-serif", 20), fg="#000000")
        self.title_label.grid(row=0, column=0, columnspan=2)

        self.world_total_label=Label(text="World Total Cases: ", font=("Arial, Helvetica, sans-serif", 15), fg="#000000")
        self.world_total_label.grid(row=1, column=0, sticky="w")
        self.world_total_entry=Entry()
        self.world_total_entry.grid(row=1, column=1, sticky="e")
        self.world_deaths_label=Label(text="World Death Cases: ", font=("Arial, Helvetica, sans-serif", 15), fg="#000000")
        self.world_deaths_label.grid(row=2, column=0, sticky="w")
        self.world_deaths_entry=Entry()
        self.world_deaths_entry.grid(row=2, column=1, sticky="w")
        self.world_reco_label=Label(text="World Recovered Cases: ", font=("Arial, Helvetica, sans-serif", 15), fg="#000000")
        self.world_reco_label.grid(row=3, column=0, sticky="w")
        self.world_reco_entry=Entry()
        self.world_reco_entry.grid(row=3, column=1, sticky="w")
        self.world_active_label=Label(text="World Active Cases: ", font=("Arial, Helvetica, sans-serif", 15), fg="#000000")
        self.world_active_label.grid(row=4, column=0, sticky="w")
        self.world_active_entry=Entry()
        self.world_active_entry.grid(row=4, column=1, sticky="w")

        self.white_space=Label(text="")
        self.white_space.grid(row=5)

        self.lebanon_total_label=Label(text="Lebanon Total Cases: ", font=("Arial, Helvetica, sans-serif", 15), fg="#000000")
        self.lebanon_total_label.grid(row=6, column=0, sticky="w")
        self.lebanon_total_entry=Entry()
        self.lebanon_total_entry.grid(row=6, column=1, sticky="w")
        self.lebanon_deaths_label=Label(text="Lebanon Deaths Cases: ", font=("Arial, Helvetica, sans-serif", 15), fg="#000000")
        self.lebanon_deaths_label.grid(row=7, column=0, sticky="w")
        self.lebanon_deaths_entry=Entry()
        self.lebanon_deaths_entry.grid(row=7, column=1, sticky="w")
        self.lebanon_reco_label=Label(text="Lebanon Recovered Cases: ", font=("Arial, Helvetica, sans-serif", 15), fg="#000000")
        self.lebanon_reco_label.grid(row=8, column=0, sticky="w")
        self.lebanon_reco_entry=Entry()
        self.lebanon_reco_entry.grid(row=8, column=1, sticky="w")
        self.lebanon_active_label=Label(text="Lebanon Active Cases: ", font=("Arial, Helvetica, sans-serif", 15), fg="#000000")
        self.lebanon_active_label.grid(row=9, column=0, sticky="w")
        self.lebanon_active_entry=Entry()
        self.lebanon_active_entry.grid(row=9, column=1, sticky="w")

        self.white_space=Label(text="")
        self.white_space.grid(row=10)

        self.iran_total_label=Label(text="Iran Total Cases: ", font=("Arial, Helvetica, sans-serif", 15), fg="#000000")
        self.iran_total_label.grid(row=11, column=0, sticky="w")
        self.iran_total_entry=Entry()
        self.iran_total_entry.grid(row=11, column=1, sticky="w")
        self.iran_deaths_label=Label(text="Iran Deaths Cases: ", font=("Arial, Helvetica, sans-serif", 15), fg="#000000")
        self.iran_deaths_label.grid(row=12, column=0, sticky="w")
        self.iran_deaths_entry=Entry()
        self.iran_deaths_entry.grid(row=12, column=1, sticky="w")
        self.iran_reco_label=Label(text="Iran Recovered Cases: ", font=("Arial, Helvetica, sans-serif", 15), fg="#000000")
        self.iran_reco_label.grid(row=13, column=0, sticky="w")
        self.iran_reco_entry=Entry()
        self.iran_reco_entry.grid(row=13, column=1, sticky="w")
        self.iran_active_label=Label(text="Iran Active Cases: ", font=("Arial, Helvetica, sans-serif", 15), fg="#000000")
        self.iran_active_label.grid(row=14, column=0, sticky="w")
        self.iran_active_entry=Entry()
        self.iran_active_entry.grid(row=14, column=1, sticky="w")

        self.white_space=Label(text="")
        self.white_space.grid(row=15)

        self.japan_total_label=Label(text="Japan Total Cases: ", font=("Arial, Helvetica, sans-serif", 15), fg="#000000")
        self.japan_total_label.grid(row=16, column=0, sticky="w")
        self.japan_total_entry=Entry()
        self.japan_total_entry.grid(row=16, column=1, sticky="w")
        self.japan_deaths_label=Label(text="Japan Deaths Cases: ", font=("Arial, Helvetica, sans-serif", 15), fg="#000000")
        self.japan_deaths_label.grid(row=17, column=0, sticky="w")
        self.japan_deaths_entry=Entry()
        self.japan_deaths_entry.grid(row=17, column=1, sticky="w")
        self.japan_reco_label=Label(text="Japan Recovered Cases: ", font=("Arial, Helvetica, sans-serif", 15), fg="#000000")
        self.japan_reco_label.grid(row=18, column=0, sticky="w")
        self.japan_reco_entry=Entry()
        self.japan_reco_entry.grid(row=18, column=1, sticky="w")
        self.japan_active_label=Label(text="Japan Active Cases: ", font=("Arial, Helvetica, sans-serif", 15), fg="#000000")
        self.japan_active_label.grid(row=19, column=0, sticky="w")
        self.japan_active_entry=Entry()
        self.japan_active_entry.grid(row=19, column=1, sticky="w")

        self.white_space=Label(text="")
        self.white_space.grid(row=20)

        self.update_bttn=Button(text="Update Numbers", command=self.update_values)
        self.update_bttn.grid(row=21, column=1, sticky="e")

    def update_values(self):
        self.world_total=self.world_total_entry.get()
        self.world_deaths=self.world_deaths_entry.get()
        self.world_reco=self.world_reco_entry.get()
        self.world_active=self.world_active_entry.get()
        self.lebanon_total=self.lebanon_total_entry.get()
        self.lebanon_deaths=self.lebanon_deaths_entry.get()
        self.lebanon_reco=self.lebanon_reco_entry.get()
        self.lebanon_active=self.lebanon_active_entry.get()
        self.iran_total=self.iran_total_entry.get()
        self.iran_deaths=self.iran_deaths_entry.get()
        self.iran_reco=self.iran_reco_entry.get()
        self.iran_active=self.iran_active_entry.get()
        self.japan_total=self.japan_total_entry.get()
        self.japan_deaths=self.japan_deaths_entry.get()
        self.japan_reco=self.japan_reco_entry.get()
        self.japan_active=self.japan_active_entry.get()
        print("Values Succesfully Updated")

        html_stat_code=f"""
        <!DOCTYPE html> etc...
        """
        self.stat_code=open("/Users/bechara/Desktop/coronaReady/website/stats_page/stats_page.html", "w")
        self.stat_code.write(html_stat_code)
        self.stat_code.close()

但是随后同时使用:]调用这两个脚本时>

from updating.update_graphs import Graph
from updating.update_numbers import NewNumbers


lebanon=Graph("Lebanon")
iran=Graph("Iran")
japan=Graph("Japan")
number=NewNumbers()
number.title("Update coronaReady")
number.configure(bg="#ffffff")
number.mainloop()

我收到帖子开头指定的错误。我该如何解决?谢谢。

我正在尝试编写一个使用输入的值更新HTML文件的脚本,该脚本分为2个python文件,每个文件更新一个特定的元素,但是随后在第三个脚本中调用了这两个文件。 ...

python python-3.x matplotlib tkinter
1个回答
0
投票

正如acw1668在我的原始帖子下评论说,将Graph()行放在mainloop()之后解决了该问题

© www.soinside.com 2019 - 2024. All rights reserved.