scotty101
Posts: 2855
Joined: Fri Jun 08, 2012 6:03 pm

Re: tkinter How to pass entry to another module?

Mon Oct 23, 2017 1:41 pm

At the moment the first code listing (server_channel.py??) does not include or call anything in the other program listing but the other program imports server_channel. I'd do this the other way round.

Inside server_channel.py do the following things
import irc_connection
create an instance of the IRC_Server class
open gui.
When the enter button is pressed, call a function which calls a method of the IRC_Server instance passing the value of the entry box as a parameter.

I'll try to create a simple example and post it here.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

scotty101
Posts: 2855
Joined: Fri Jun 08, 2012 6:03 pm

Re: tkinter How to pass entry to another module?

Mon Oct 23, 2017 1:59 pm

Ok a simple dumbed down example so I am not doing all the work for you.

gui.py

Code: Select all

#!/usr/bin/python 3.6
from tkinter import *
from chat_client import ChatClient

def server_get(event):
    text = entry.get()
    client.send_message(text)


client = ChatClient()
client.connect()

root = Tk()
v = StringVar()
text1 = Text(root, height=25, width=125, bg='black', fg='yellow')
scroll = Scrollbar(root, command=text1.yview, orient=VERTICAL)
scroll.config(command=text1.yview)
text1.configure(yscrollcommand=scroll.set)
entry = Entry(root, textvariable = v, bg='white', fg='black')
entry.pack(side = BOTTOM, fill=BOTH)
entry.focus()
text1.pack(side=LEFT, fill=BOTH, expand='YES')
scroll.pack(side=RIGHT, fill=BOTH)
entry.bind('<Return>', server_get)

root.mainloop()
chat_client.py

Code: Select all

class ChatClient:
     def __init__(self):
        print('Created Chat Client')
        
     def connect(self):
        print('Connected')

     def send_message(self, data):
        print("Client sent '{}'".format(data))
The chat_client code has no awareness of the gui code but the gui code creates an instance of my "ChatClient" and is able to send messages to it. It is good design practice to not mix up GUI and 'back-end' functionality, this makes your code more testable and less subject to change if you were to change the layout of the GUI.

PS. I didn't change much of the gui code that you created but if I was doing this, I would make more use of objects and classes to do this.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

scotty101
Posts: 2855
Joined: Fri Jun 08, 2012 6:03 pm

Re: tkinter How to pass entry to another module?

Thu Nov 23, 2017 2:03 pm

Share the contents of irc_connection2.py and I might be able to help.

irc_connection2.py should be in the same folder as the python program trying to import it.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

scotty101
Posts: 2855
Joined: Fri Jun 08, 2012 6:03 pm

Re: tkinter How to pass entry to another module?

Thu Nov 23, 2017 3:06 pm

If I copy the contents of your irc_connection2.py file and try to import it from another python script, I don't get any errors.
The only way I can replicate the same issue as you is to remove the code from irc_connection2.py.

My suggestion would be to double and triple check that the python file name matches what you have on your "from irc_connection2 import IRC_Server" line.
Also perhaps you have another file with the same name that it is importing instead. To check which file you are importing, try creating a new python script in the same folder with the following code.

Code: Select all

import irc_connection2

print(str(irc_connection2))
print("Done")
For me this reports

Code: Select all

<module 'irc_connection2' from 'C:\\Python34\\Projects\\RaspberryPi\\IRC\\irc_connection2.py'>
Done
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

Return to “Graphics programming”

Who is online

Users browsing this forum: No registered users and 2 guests