Hi I am trying to create a simple program that writes a few lines to a file then displays those lines in labels below. The labels don't update though when I update the file and I have tried
but it doesn't work. Here is my code: -
from Tkinter import *
-
import sys
-
f=open("stats")
-
line1=f.readline()
-
line2=f.readline()
-
line3=f.readline()
-
line4=f.readline()
-
line5=f.readline()
-
line6=f.readline()
-
line7=f.readline()
-
line8=f.readline()
-
line9=f.readline()
-
line10=f.readline()
-
-
def write():
-
f=open("stats", "w")
-
f.write(e1.get())
-
f.write(" ")
-
f.write(e2.get())
-
f.write("\n")
-
e1.delete(0, END)
-
e2.delete(0, END)
-
-
root=Tk()
-
root.title("Basketball Stats")
-
l1=Label(text="Player").grid(column=0, row=0)
-
l2=Label(text="Number").grid(column=0, row=1)
-
e1=Entry().grid(column=1, row=0)
-
e2=Entry().grid(column=1, row=1)
-
b1=Button().grid(column=1, row=3)
-
b1.config(text="Submit", command=write)
-
l3=Label(text=line1).grid(column=0, row=4)
-
l4=Label(text=line2).grid(column=0, row=5)
-
l5=Label(text=line3).grid(column=0, row=6)
-
l6=Label(text=line4).grid(column=0, row=7)
-
l7=Label(text=line5).grid(column=0, row=8)
-
l8=Label(text=line6).grid(column=0, row=9)
-
l9=Label(text=line7).grid(column=0, row=10)
-
l10=Label(text=line8).grid(column=0, row=11)
-
l11=Label(text=line9).grid(column=0, row=12)
-
l12=Label(text=line10).grid(column=0, row=13)
-
root.mainloop()
-
1 1756 dwblas 626
Recognized Expert Contributor
We don't have the file so don't know what it contains, nor did you say how you want the labels updated. Initially you open "stats" as read only and never close it. When you open it again in the write() function, the result is unpredictable because the file is already open. The wite() function's file pointer is garbage collected/closed when the function exits. Later, the file pointer from the original open is closed when the program exits, so the file remains the same because it (probably) keeps the original dimensions from the first file pointer, i.e. truncates the new data. Note that "l1" can look like 11 or ll in some type fonts. - from Tkinter import *
-
-
## write a test file
-
fname="./stats_2"
-
with open(fname, "w") as f:
-
for ctr in range(1, 11):
-
f.write("line %d\n" % (ctr))
-
-
records=[]
-
with open(fname, "r") as f:
-
for ctr in range(10):
-
records.append(f.readline().strip())
-
-
def write():
-
first=e1.get()
-
second=e2.get()
-
with open(fname, "a") as f: ## append not (over)write
-
f.write("%s %s\n" % (first, second))
-
e1.delete(0, END)
-
e2.delete(0, END)
-
-
## update any labels for an example
-
l1["text"]=first
-
l1["bg"]="lightblue"
-
l2["text"]=second
-
l2["bg"]="lightyellow"
-
-
root=Tk()
-
root.title("Basketball Stats")
-
-
## in your code, "l1" (and everything else) is None
-
## because it contains the return from grid(), which is None
-
l1=Label(text="Player")
-
l1.grid(column=0, row=0)
-
l2=Label(text="Number")
-
l2.grid(column=0, row=1)
-
e1=Entry(root)
-
e1.grid(column=1, row=0)
-
e2=Entry(root)
-
e2.grid(column=1, row=1)
-
b1=Button(root)
-
b1.grid(column=1, row=3)
-
b1.config(text="Submit", command=write)
-
Button(root, text="quit", command=root.quit, bg="orange"). grid(column=1, row=4)
-
-
r=4
-
for ctr in range(10):
-
## this line returns None so no point in catching it
-
Label(root, text=records[ctr]).grid(column=0, row=r+ctr)
-
-
root.mainloop()
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Shane |
last post by:
I would like to update Access by using a txtBox.
Is it possible to update Access this way?
Example:
If I selected the txtBox and input a line of text I would like that line of
text to be sent...
|
by: kareemaffan |
last post by:
Hello Everyone
I want to insert values into MS Access database through VB.NET in Visual Studio 2005 . I have used the following code which is not working.
Private Sub Button2_Click(ByVal sender...
|
by: rameshgohil |
last post by:
I am using grid view and a button column in it using <itemTemplate>
but I am not able to rerive cell value of a selected row from grid view.
I have tried the following to methods in Row_command...
|
by: ykhamitkar |
last post by:
Hi there,
I have a form where I want to add new row using button and done want a default new record,
I have changed "Allow Additions" to No for the forms properties
and have added following...
|
by: KKSohaib |
last post by:
how I update sql table using datagridview cell..... I want to edit cell and then press enter it update the sql table ... ??? It possible or not ... ?
| |
by: atultaj |
last post by:
without using button how we can insert the value in databas using textbox
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |